Issue with cascade deletion

17 views
Skip to first unread message

Javier Uribe

unread,
Nov 10, 2023, 10:45:49 AM11/10/23
to sqlalchemy
Hi, so I have the database classes as follows:

class Thing(Base):


DatasetId = Column(ForeignKey(schema + 'Dataset.DatasetId'), primary_key=True, nullable=False, index=True)

RequiredThing_child = relationship('RequiredThing', cascade='all, delete-orphan')

class Location(Base):


DatasetId = Column(ForeignKey(schema + 'Dataset.DatasetId'), primary_key=True, nullable=False, index=True)

RequiredThing_child = relationship('RequiredThing', cascade='all, delete-orphan')

class RequiredThing(Base):


DatasetId = Column(ForeignKey(schema + 'Dataset.DatasetId'), primary_key=True, nullable=False, index=True)

Thing= relationship('Thing', viewonly=True) Location= relationship('Location', viewonly=True)

(There are more relationships on both tables but these ones are the only ones that I've had issues with, as they both have that column).  


I set them up like that because I want to have the cascade deletion functionality: whenever I call for a deletion of a thing object I want the children (in this case RequiredThing) to be deleted as well (as long as it matches with the Thing Id). When I had it active for only one table (in this case Thing), it worked fine but now that I also added the relationship for the Location table it's giving me the following warning: 

SAWarning: relationship 'Location.RequiredThing_child' will copy column Location.DatasetId to column RequiredThing.DatasetId, which conflicts with relationship: 'Thing.RequiredThing_child' (copies Thing.DatasetId to RequiredThing.DatasetId).

I've tried to add back_populates to the RequiredThing class but to no avail.
I need this to work because I want to implement the cascade functionality to many more tables as needed but I do not want there to be any errors or ambiguity when running the queries.
Or if there's a better way to implement cascade deletion I'm all for it.
Any suggestions or corrections to my code would be greatly appreciated!

Kind regards,

Javier

(reposted to edit the paragraphs)
Reply all
Reply to author
Forward
0 new messages