title: SQLAlchemy cascading deletes to many to many table
body: <p>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):</p>
  <pre class="lang-py prettyprint-override"><code>
  class Key(Model):
     ...
     values = relationship('Value', back_populates='key', cascade='all, delete-orphan')
  
  class Value(Model):
     
     value = Column(Text, nullable=False)
     key = relationship(&quot;Key&quot;, back_populates=&quot;values&quot;)
     key_id = Column(Integer, ForeignKey(&quot;keys.id&quot;))
  
  
  class Foo(Model):
  
     ...
     taglist = db.relationship(&quot;Value&quot;, 
                                 order_by=&quot;desc(Value.created)&quot;, 
                                 secondary=association_table, 
                                 lazy='dynamic')
  </code></pre>
  <p>And the <code>association_table</code> is just a classic FK&lt;--&gt;FK table:</p>
  <pre class="lang-py prettyprint-override"><code>Table(&quot;association_table&quot;, 
     Model.metadata,
     Column(&quot;foo.id&quot;, Integer, ForeignKey(&quot;foos.id&quot;),
     Column(&quot;value.id&quot;, Integer, ForeignKey(&quot;values.id&quot;),)
  </code></pre>
  <p>With this setup, using <code>postgres</code>, when Im trying to delete a Key, I get the following:
  <code>(psycopg2.errors.ForeignKeyViolation) update or delete on table &quot;values&quot; violates foreign key constraint &quot;association_table_value_id_fkey&quot; on table &quot;association_table&quot;</code></p>
  <p>Now, reading through the <a href="https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#relationships-many-to-many-deletion" rel="nofollow noreferrer">https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#relationships-many-to-many-deletion</a> documentation I've tried the examples there which mostly seem to say put an <code>ondelete=&quot;CASCADE&quot;</code> on your association table, however this results in the same issue.</p>
  <p>Is there a way I can do what I want, e.g. &quot;unlink&quot; the foo objects from the MTM table (without deleting <em>them</em>) ?</p>
  <p>Thanks in advance for any help</p>