Hello,

I have two tables in PostgreSQL where one is INHERITed from the other:

CREATE TABLE a
(
    pk serial,
    type_pk integer,
    value numeric,
    CONSTRAINT a_pkey PRIMARY KEY (pk),
    CONSTRAINT a_type_fk FOREIGN KEY (a_type_pk)
        REFERENCES a_type (pk) MATCH SIMPLE
        ON UPDATE CASCADE
        ON DELETE RESTRICT
);


CREATE TABLE type
(
    pk serial,
    label text
);


CREATE TABLE b
(
    pk ,
    type_pk ,
    value ,
    b_value numeric
)
INHERITS (a);

I have my SQLAlchemy classes defined like this:

class Type(Base):
    __tablename__ = 'a_type'
    __table_args__ = {'autoload':True, 'schema':'public'}


class A(Base):
    __tablename__ = 'a'
    __table_args__ = {'autoload':True, 'schema':'public'}


    type_pk = Column("type_pk", Integer, ForeignKey(Type.pk))


    type = relationship(Type,
                        primaryjoin=Type.pk==type_pk,
                        backref="as")


    __mapper_args__ = {
        'polymorphic_identity':"a",
        'polymorphic_on':case([
            (type_pk == 1, "b") 
        ], else_="a")
    }
    
class B(A):
    __tablename__ = 'b'
    __table_args__ = {'autoload':True, 'schema':'public'}


    pk = Column(name='pk', type_=Integer, primary_key=True, autoincrement=
True)
    __mapper_args__ = {
        'polymorphic_identity': "b",
        'inherit_condition':pk==A.pk,
        'inherit_foreign_keys':['type_pk']
    }



This very nearly works and I get the polymorphism I am looking for. When I 
create a new B() object in code and look at the SQL generated, it triggers 
two inserts - one for the 'a' table, and one for the 'b' table. How can I 
suppress the INSERT into 'a'? With PostgreSQL's INHERIT, I only need the 
'b' INSERT. This works very well otherwise.

I get a few of these warnings. Is there a configuration I need to specify 
to tell SQLAlchemy not to worry about it?

/usr/local/anaconda/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py:1766: 
> SAWarning: Implicitly combining column a.pk with column b.pk under 
> attribute 'pk'.  Please configure one or more attributes for these 
> same-named columns explicitly.


Finally, is there a way I can automatically set a default value of 1 for 
the 'type_pk' propoerty when a new B() is created? I don't know if I can do 
this in PostgreSQL since the field is inherited.

Thanks!

Demitri

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to