"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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---