SQLAlchemy cascades deletes to collections associated with objects that are 
present in the Session.   Not every database supports cascading of foreign 
keys.      I didn't introduce this concept in my last email, but if you 
configure foreign key level cascades, and your application is only to be used 
on a cascade-supporting backend, you also can configure passive_deletes=True so 
that SQLA won't go through the effort of loading in collections to be deleted 
that aren't already present.

http://www.sqlalchemy.org/docs/orm/session.html#deleting
http://www.sqlalchemy.org/docs/orm/collections.html#using-passive-deletes
 


On Feb 25, 2011, at 11:09 AM, Eric Ongerth wrote:

> Wouldn't he need to configure the "ondelete" cascade for even
> "session.delete(session.query(User).get('testname'))"  to work that
> way?
> 
> I know why the cascade is necessary for session.query(User).delete()
> to also delete the associated IP instances.  But I don't quite get why
> it's not necessary for that other method of deleting the user to
> delete the associated IPs.
> 
> 
> On Feb 25, 7:17 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> On Feb 25, 2011, at 7:31 AM, Chris Withers wrote:
>> 
>> 
>> 
>>> Hi All,
>> 
>>> I have the following models:
>> 
>>> class IP(Base):
>>>    __tablename__ = 'ip'
>>>    username = Column(String(50),
>>>                      ForeignKey('user.username'),
>>>                      primary_key=True)
>>>    ip = Column(String(15), primary_key=True, index=True)
>> 
>>> class User(Base):
>>>    __tablename__ = 'user'
>>>    username = Column(String(50), primary_key=True)
>>>    ips = relation("IP",
>>>                   order_by="IP.ip",
>>>                   backref="user",
>>>                   cascade="all")
>> 
>>> If I delete a user as follows:
>> 
>>> session.delete(session.query(User).get('testname'))
>> 
>>> ...then the IPs associated with 'testname' get deleted.
>> 
>>> However, if I do:
>> 
>>> session.query(User).delete()
>> 
>>> ..they do not.
>> 
>>> Why is that?
>>> How do I get them both to work?
>> 
>> you would need to configure "ON DELETE CASCADE" on the foreign key.  
>> ForeignKey() offers the "ondelete" option for this.
>> 
>> 
>> 
>>> cheers,
>> 
>>> Chris
>> 
>>> --
>>> Simplistix - Content Management, Batch Processing & Python Consulting
>>>           -http://www.simplistix.co.uk
>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To post to this group, send email to sqlalchemy@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> sqlalchemy+unsubscr...@googlegroups.com.
>>> For more options, visit this group 
>>> athttp://groups.google.com/group/sqlalchemy?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to