Deleting members of a relation() collection via del causes constraint violations.
Issue #1693
resolved
Consider the following:
from sqlalchemy import create_engine,Column,Integer,ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relation
from sqlalchemy.orm.interfaces import AttributeExtension
c=create_engine("postgresql://bryant:uugoh40u@localhost/antekdb",echo=True)
Base=declarative_base()
Base.metadata.bind=c
Session=sessionmaker(bind=c)
class B(Base):
__tablename__="b"
id=Column(Integer,primary_key=True)
aid=Column(Integer,ForeignKey("a.id"),nullable=False)
class A(Base):
__tablename__="a"
id=Column(Integer,primary_key=True)
b=relation("B",backref="a")
Base.metadata.drop_all()
Base.metadata.create_all()
s=Session()
a=A()
a.b=[B(),B(),B()](B(),B(),B())
s.add(a)
s.commit()
a=s.query(A).first()
## impending weirdness
## what I want
for i in a.b:
s.delete(i);
## integrity error
del a.b[:](:)
s.commit()
Is it an intended feature for "del a.b..." to generate UPDATE statements with NULL values in the ForeignKey? If one is not supposed to del a.b:, then perhaps an error should be generated on the sqlalchemy level, instead of relying on the database to squawk?
Comments (3)
-
Account Deleted -
repo owner you're looking for "cascade='all, delete-orphan'", documented at http://www.sqlalchemy.org/docs/ormtutorial.html#configuring-delete-delete-orphan-cascade as well as http://www.sqlalchemy.org/docs/session.html#cascades. assuming that solves your issue this ticket should be closed.
-
repo owner - changed status to wontfix
closing for now.
- Log in to comment
Minus the personally identifying create_engine params.