I have a situation where i want to upon deletion of a Key (Parent) delete all it's Values (children), and the related rows in a MTM association between Children and Foo. The models are as follows (some attribs removed):
class Key(Model):
...
values = relationship('Value', back_populates='key', cascade='all, delete-orphan')
class Value(Model):
value = Column(Text, nullable=False)
key = relationship("Key", back_populates="values")
key_id = Column(Integer, ForeignKey("keys.id"))
class Foo(Model):
...
taglist = db.relationship("Value",
order_by="desc(Value.created)",
secondary=association_table,
lazy='dynamic')
And the association_table is just a classic FK<-->FK table:
Table("association_table",
Model.metadata,
Column("foo.id", Integer, ForeignKey("foos.id"),
Column("value.id", Integer, ForeignKey("values.id"),)
With this setup, using postgres, when Im trying to delete a Key, I get the following:
(psycopg2.errors.ForeignKeyViolation) update or delete on table "values" violates foreign key constraint "association_table_value_id_fkey" on table "association_table"
Now, reading through the https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#relationships-many-to-many-deletion documentation I've tried the examples there which mostly seem to say put an ondelete="CASCADE" on your association table, however this results in the same issue.
Is there a way I can do what I want, e.g. "unlink" the foo objects from the MTM table (without deleting them) ?
Thanks in advance for any help