Hi, I have a strange (for me) problem with backref.
SQLAlchemy version is 0.3.1-1 on Debian Etch.
Here is the code:
from sqlalchemy import *
db = create_engine('postgres://manlio:[EMAIL PROTECTED]/test')
metadata = BoundMetaData(db)
a = Table(
'a', metadata,
Column('x', String),
Column('y', String),
PrimaryKeyConstraint('x', 'y')
)
b = Table(
'b', metadata,
Column('x', String),
Column('y', String),
Column('z', String),
PrimaryKeyConstraint('x', 'y'),
ForeignKeyConstraint(['x', 'y'], ['a.x', 'a.y'])
)
metadata.create_all()
class A(object):
def __init__(self, x, y):
self.x = x
self.y = y
class B(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
mapper(A, a)
mapper(
B, b,
properties={
'a': relation(
A, backref=backref('b', lazy=False, uselist=False,
cascade='all, delete-orphan'),
uselist=False, cascade='all, delete-orphan'
)
}
)
try:
conn = db.connect()
trans = conn.begin()
sess = create_session()
o = A('1', '2')
o.b = B(o.x, o.y, '3')
sess.save(o)
sess.flush()
sess.close()
trans.commit()
trans = conn.begin()
sess = create_session()
sess.update(o)
print o.b.z
del o.b
sess.flush()
sess.close()
trans.commit()
print o.b
s = a.select()
print conn.execute(s).fetchall()
finally:
metadata.drop_all()
The problem here is that when I delete o.b, SQLAlchemy deletes the rows
from 'a' table, too.
Moreover the programs blocks at metadata.drop_all().
Now:
1) when I remove the cascade rule from backref I obtain:
sqlalchemy.exceptions.AssertionError: Dependency rule tried to
blank-out primary key column 'b.x' on instance '[EMAIL PROTECTED]'
2) when I remove the cascade rule from relation I obtain:
sqlalchemy.exceptions.InvalidRequestError: Parent instance <class
'__main__.A'> is not bound to a Session, and no contextual session is
established; lazy load operation of attribute 'b' cannot proceed
3) when I remove the cascade rule from both backref and relation, I
obtain:
sqlalchemy.exceptions.InvalidRequestError: Parent instance <class
'__main__.A'> is not bound to a Session, and no contextual session is
established; lazy load operation of attribute 'b' cannot proceed
4) if I set the cascade rule to "all" I obtain:
sqlalchemy.exceptions.InvalidRequestError: Instance '<__main__.B object
at 0xb794068c>' is not attached or pending within this session
What's the problem?
Thanks 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
-~----------~----~----~----~------~----~------~--~---