[sqlalchemy] Re: Allowing orphaned children

2009-02-07 Thread Michael Bayer

cascade=all includes delete cascade.   any Hat objects attached to  
User will be deleted when the User is deleted.To resolve, leave  
the cascade argument out.  it defaults to save-update, merge which  
is enough for most use cases.


On Feb 6, 2009, at 11:05 PM, James wrote:


 Hi, I'm trying to set up a model where child objects are allowed to
 not have parents. At present, I can't get SA to leave the children
 intact, despite having ondelete=SET NULL and no delete-orphans.

 This is with SA 0.4.3.

 To demonstrate my confusion, can someone explain why this code deletes
 all my hats:

 import sys, time
 from datetime import datetime
 from sqlalchemy import Table, Column, ForeignKey, MetaData,
 create_engine
 from sqlalchemy.orm import relation, sessionmaker, mapper, backref
 from sqlalchemy import String, Unicode, Integer, DateTime

 metadata=MetaData()
 engine = create_engine(sqlite:///:memory:)

 users_table = Table('tg_user', metadata,
Column('user_id', Integer, primary_key=True),
 )

 hat_table = Table('hat', metadata,
Column('id', Integer, primary_key=True),
Column('user_id', Integer, ForeignKey('tg_user.user_id',
 ondelete='SET NULL')),
 )

 metadata.create_all(engine)

 class User(object):
pass

 class Hat(object):
pass

 mapper(User, users_table)

 mapper(Hat, hat_table,
properties = {
'user': relation(User, backref=backref(hats,
 cascade=all)),
}
 )

 Session = sessionmaker(bind=engine, autoflush=False,
 transactional=True)
 session = Session()

 me = User()
 me.hats.extend([Hat(), Hat(), Hat()])
 session.save(me)
 session.flush()

 print session.query(Hat).count(), hats
 session.delete(me)
 session.flush()
 print session.query(Hat).count(), hats

 Thank you!
 James
 


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Allowing orphaned children

2009-02-07 Thread James

Oh, of course - thanks Michael!

On Feb 7, 1:51 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 cascade=all includes delete cascade.   any Hat objects attached to  
 User will be deleted when the User is deleted.    To resolve, leave  
 the cascade argument out.  it defaults to save-update, merge which  
 is enough for most use cases.

 On Feb 6, 2009, at 11:05 PM, James wrote:



  Hi, I'm trying to set up a model where child objects are allowed to
  not have parents. At present, I can't get SA to leave the children
  intact, despite having ondelete=SET NULL and no delete-orphans.

  This is with SA 0.4.3.

  To demonstrate my confusion, can someone explain why this code deletes
  all my hats:

  import sys, time
  from datetime import datetime
  from sqlalchemy import Table, Column, ForeignKey, MetaData,
  create_engine
  from sqlalchemy.orm import relation, sessionmaker, mapper, backref
  from sqlalchemy import String, Unicode, Integer, DateTime

  metadata=MetaData()
  engine = create_engine(sqlite:///:memory:)

  users_table = Table('tg_user', metadata,
     Column('user_id', Integer, primary_key=True),
  )

  hat_table = Table('hat', metadata,
     Column('id', Integer, primary_key=True),
     Column('user_id', Integer, ForeignKey('tg_user.user_id',
  ondelete='SET NULL')),
  )

  metadata.create_all(engine)

  class User(object):
     pass

  class Hat(object):
     pass

  mapper(User, users_table)

  mapper(Hat, hat_table,
     properties = {
         'user': relation(User, backref=backref(hats,
  cascade=all)),
     }
  )

  Session = sessionmaker(bind=engine, autoflush=False,
  transactional=True)
  session = Session()

  me = User()
  me.hats.extend([Hat(), Hat(), Hat()])
  session.save(me)
  session.flush()

  print session.query(Hat).count(), hats
  session.delete(me)
  session.flush()
  print session.query(Hat).count(), hats

  Thank you!
  James
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---