about cascade rule
Manlio Perillo <manlio.perillo <at> gmail.com>
2007-02-01 12:24:35 GMT
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:manlio <at> localhost/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)
manlio <at> synapsis:~/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 <at> googlegroups.com
To unsubscribe from this group, send email to sqlalchemy-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---