On 02/02/2016 04:27 PM, Brian Leach wrote:
This question is sparked by some trouble that I am having with deleting
model instances. Please see this
question: 
http://stackoverflow.com/questions/35163325/sqlalchemy-circular-dependency-on-delete

I have set up my models like below. I have several kinds of "tests"
defined in my database. Each "test" shares certain columns like
'status', 'date', 'who preformed it', etc. Each different test type then
defines columns specific to it.

|classHasID(object):@declared_attrdefid(cls):returnColumn('id',Integer,Sequence('test_id_seq'),primary_key=True)...classTestParent(HasID,Model)__tablename__
='tests'discriminator =Column(String(50))__mapper_args__
={'polymorphic_on':discriminator}...classFooTest(TestParent,Model):__tablename__
='footests'__mapper_args__ ={'polymorphic_identity':'footests'}id
=Column(Integer,ForeignKey('tests.id'),primary_key=True)parent_id
=Column(Integer,ForeignKey('footests.id'))children
=relationship('FooTest',foreign_keys='FooTest.id',lazy='joined',join_depth=2,cascade='save-update,
merge, delete,
delete-orphan')...classBarTest(TestParent,Model):__tablename__
='bartests'__mapper_args__ ={'polymorphic_identity':'bartests'}id
=Column(Integer,ForeignKey('tests.id'),primary_key=True)...|



Now, I am wondering if this is a correct way to set up the FooTest, as
one instance of FooTest may have several child instances of FooTest as
children.

I am unable to delete any test instance (BarTest or otherwise) and am
getting a circular dependency error referring to the FooTest table.

I think the relationship on FooTest needs to be:

         children = relationship('FooTest',
                            foreign_keys='FooTest.parent_id',
                            lazy='joined',
                            join_depth=2,
cascade='save-update, merge, delete, delete-orphan')


"foreign_keys" here refers to which column has the ForeignKey constraint on it within the relationship. For a self-referential relationship there is also the concept of "remote_side", which in this case would be "FooTest.id", however this will be the default setup here as it assumes one-to-many by default.




Am I missing any fundamental concepts related to table inheritance?


Thanks everyone,

Brian Leach

--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to