I am getting an error where delete orphan on a relationship seems to happen 
even when the parent object is expunged.

I reproduced the issue in this example.

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import *

Base = declarative_base()

class Parent(Base):
__tablename__ = 'parent'

id = Column(Integer, primary_key=True)

children = relationship('Child', lazy='joined', backref='parent', 
cascade='all, delete-orphan')

def dict(self):
return {
'id': self.id,
'children': [c.dict() for c in self.children]
}

class Child(Base):
__tablename__ = 'child'

parent_id = Column(None, ForeignKey(Parent.id), primary_key=True)
name = Column(String(8), primary_key=True)

def dict(self):
return {
'name': self.name
}

if __name__ == '__main__':
e = create_engine('sqlite:///orphan.db', echo=True)

Base.metadata.drop_all(e)
Base.metadata.create_all(e)

s = sessionmaker(e)()

p = Parent()
p.children = [Child(name=str(i)) for i in range(5)]

s.add(p)
s.commit()

p = s.query(Parent).one()
print(p.dict())

p.children = [Child(name=str(i)) for i in range(5, 10)]

print(p.dict())

s.expunge(p)

p = s.query(Parent).one()
print(p.dict())

The parent has no children after the expunge, is this intended or a bug?

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to