This is not a problem, I just want to understand. Given the following code:
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import * from sqlalchemy.orm import sessionmaker, relationship Base = declarative_base() class AB(Base): __tablename__= 'ab' id_a = Column(Integer, ForeignKey('a.id', ondelete='CASCADE'), primary_key=True) id_b = Column(Integer, ForeignKey('b.id', ondelete='CASCADE'), primary_key=True) rel = Column(Unicode) class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True)
I want the records from table AB deleted when deleting related records from B I tried 3 types of relationships (check table B):
1: Does not work (AssertionError: Dependency rule tried to exclude an empty primary key column 'ab.id_b' per instance ''), whereas if you try to delete it directly in the database, the restrictions will be used correctly and the records from AB will be deleted.
2: It works, I don’t understand why this is necessary, because the generated databases are identical (you can check the diff in the output)
3: works, DB constraints do the work.
Leaving (3) separately, I don’t understand why (2) is necessary, because ondelete='cascade' already installed, and the generated database is identical. My guess would be with (1), SQLAlchemy has enough information to have the correct behavior.
Am I missing something? Thanks.
tonio
source share