Thanks so much - I realise now that the documentation I should have been reading was:
http://www.sqlalchemy.org/docs/orm/relationships.html#backref-arguments Ben On 31 August 2011 22:50, Michael Bayer <mike...@zzzcomputing.com> wrote: > "dependency rule tried to blank out primary key" means: > > 1. A references B, B has a foreign key to A. > > 2. A is deleted. > > 3. each B associated with A must therefore have the foreign key of A set to > NULL (this is the default behavior if 'delete' cascade isn't configured). > > 4. the foreign key on B is also part of the primary key. SQLA detects this > and raises an error. > > The solution, B must also be deleted when A is deleted. > > In your example, you're probably trying to do this as I see cascade="all, > delete-orphan". Except this cascade must go on the side that points A to B, > in this case ObjectA.associations and ObjectB.associations (put it another > way - the side that does *not* have the foreign key on it). Since > "associations" is on the "backref" side of the declaration here, use the > backref() function and put "cascade='all, delete-orphan'" on that side. > > > On Aug 31, 2011, at 7:04 AM, Benjamin Sims wrote: > > Hi, > > I'm still having troubles correctly figuring my various many-to-many > relations. Originally, this was a 'standard' many-to-many; that is, a > secondary table was specified. > > However, I now need to add further information to the relation and am > converting into an AssociationObject. My problem is in ensuring that the > original calls still work. So, currently I have: > > class Association(Base): > > __tablename__ = 'objecta_objectb' > objecta_id = Column(Integer, ForeignKey('objecta.id'), > primary_key=True) > objectb_id = Column(Integer, ForeignKey('objectb.id'), > primary_key=True) > objecta = relationship("ObjectA", backref = "associations", cascade = > 'all, delete-orphan') > objectb = relationship("ObjectB", backref = "associations", cascade = > 'all, delete-orphan') > > def __init__(self, objecta = None, objectb = None): > self.objecta = objecta > self.objectb = objectb > pass > > class ObjectA(Base): > objectbs = association_proxy('associations', 'objectb', creator=lambda > b: > Association(objectb=b)) > > class ObjectB(Base): > objectas = association_proxy('associations', 'objecta', creator=lambda > a: > Association(objecta=a)) > > The point of all this is so that I can create the associations by doing: > > objecta_instance.objectbs.append(objectb_instance) > > or > association_instance = Association(objectb = objectb_instance) > objecta_instance.associations.append(association_instance) > > Sorry if the above is rather convoluted; it is based on > http://www.preetk.com/node/sqlalchemy-part-2-declarative-bi-directional-association-classes/, > which is somewhat clearer. > > Anyway, using the above I get: > > AssertionError: Dependency rule tried to blank-out primary key column > 'objecta_objectb.objectb_id' on instance '<Association at 0x109277b90>' > > What am I doing wrong? I understand that it is somehow related to how the > cascade is set up, but I'm not sure how to move forward. > > Thanks, > Ben > > > > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.