[sqlalchemy] Re: about cascade rule

2007-02-01 Thread Dennis



On Feb 1, 5:24 am, Manlio Perillo [EMAIL PROTECTED] wrote:
 Hi.

 I still do not fully understand cascade rules, however I want to be sure
 the behaviour below is a feature and not a bug.


Did you intend for the B object to be able to not exist?  I modified
your code to create a b object as well as obj and the transaction
worked:

 obj = A('x')


 bobj=B('hi') # I modified your constructor to not take the id as
well
 bobj.a=obj

 sess.save(obj)

I'm not sure if it is a bug or feature either but perhaps a different
cascade rule will alter the behavior.

-Dennis


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: about cascade rule

2007-02-01 Thread Michael Bayer

delete-orphan on the A relationship means, no A is allowed to
exist in the database without a parent B.  the rule used to be more
lax and would only fire off when an A was removed from a B...but that
missed a lot of cases.   the specificness of the error message
indicates its definitely not a bug :)


On Feb 1, 7:24 am, Manlio Perillo [EMAIL PROTECTED] wrote:
 Hi.

 I still do not fully understand cascade rules, however I want to be sure
 the behaviour below is a feature and not a bug.

 Here is the code:

 from sqlalchemy import *

 db = create_engine('postgres://manlio:[EMAIL PROTECTED]/test', echo=False)

 metadata = BoundMetaData(db)
 a = Table(
  'a', metadata,
  Column('id', Integer, primary_key=True),
  Column('x', String)
  )

 b = Table(
  'b', metadata,
  Column('uid', String, primary_key=True),
  Column('id', Integer, ForeignKey(a.c.id)),
  Column('y', String)
  )

 class A(object):
  def __init__(self,  x):
  self.x = x

 class B(object):
  def __init__(self, id, y):
  self.id = id
  self.y = y

 aMapper = mapper(A, a)

 bMapper = mapper(
  B, b,
  properties={
  'a': relation(
  A, lazy=False, cascade='all, delete-orphan'
  )
  }
  )

 try:
  metadata.create_all()

  conn = db.connect()
  trans = conn.begin()
  sess = create_session(bind_to=conn)

  obj = A('x')
  sess.save(obj)

  sess.flush()
  sess.close()
  trans.commit()
  conn.close()
 finally:
  metadata.drop_all()

 When executing this script I obtain:

 Traceback (most recent call last):
File cascade.py, line 52, in ?
  sess.flush()
File /usr/lib/python2.4/site-packages/sqlalchemy/orm/session.py,
 line 220, in flush
  self.uow.flush(self, objects)
File /usr/lib/python2.4/site-packages/sqlalchemy/orm/unitofwork.py,
 line 175, in flush
  if object_mapper(obj)._is_orphan(obj):
File /usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py,
 line 232, in _is_orphan
  raise exceptions.FlushError(instance %s is an unsaved, pending
 instance and is an orphan (is not attached to %s) %
 sqlalchemy.exceptions.FlushError: instance __main__.A object at
 0xb79d946c is an unsaved, pending instance and is an orphan (is not
 attached to any parent 'B' instance via that classes' 'a' attribute)
 [EMAIL PROTECTED]:~/projects/bugs/sqlalchemy$ python cascade.py
 Traceback (most recent call last):
File cascade.py, line 52, in ?
  sess.flush()
File /usr/lib/python2.4/site-packages/sqlalchemy/orm/session.py,
 line 220, in flush
  self.uow.flush(self, objects)
File /usr/lib/python2.4/site-packages/sqlalchemy/orm/unitofwork.py,
 line 175, in flush
  if object_mapper(obj)._is_orphan(obj):
File /usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py,
 line 232, in _is_orphan
  raise exceptions.FlushError(instance %s is an unsaved, pending
 instance and is an orphan (is not attached to %s) %
 sqlalchemy.exceptions.FlushError: instance __main__.A object at
 0xb79a146c is an unsaved, pending instance and is an orphan (is not
 attached to any parent 'B' instance via that classes' 'a' attribute)

 Thanks and regards  Manlio Perillo


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: about cascade rule

2007-02-01 Thread Manlio Perillo

Michael Bayer ha scritto:
 delete-orphan on the A relationship means, no A is allowed to
 exist in the database without a parent B.  the rule used to be more
 lax and would only fire off when an A was removed from a B...but that
 missed a lot of cases.   the specificness of the error message
 indicates its definitely not a bug :)
 

Ok, thanks.
However this is not well documented in
http://www.sqlalchemy.org/docs/unitofwork.myt#unitofwork_cascade



Regards  Manlio Perillo

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---