Hi
I'm trying to merge objects across sessions and I'm seeing some odd
behavour with a one-one child relation:
"""
import sqlalchemy as sql
import sqlalchemy.orm as orm
from sqlalchemy.ext.declarative import declarative_base
engine = sql.create_engine('sqlite:///:memory:')
metadata = sql.MetaData(bind=engine)
DB = orm.sessionmaker(bind=engine, autoflush=False)
class _base(object):
def __repr__(o):
return "<%s 0x%x id=%r>" % (type(o).__name__, id(o), o.id)
base = declarative_base(metadata=metadata, cls=_base)
class Parent(base):
__tablename__ = 'a'
id = sql.Column(sql.Integer(), nullable=False, primary_key=True)
child = orm.relation("Child1", uselist=False, cascade="all,delete-
orphan")
children = orm.relation("Child2", uselist=True,
cascade="all,delete-orphan")
class Child1(base):
__tablename__ = 'b'
id = sql.Column(sql.Integer(), nullable=False, primary_key=True)
p_id = sql.Column(sql.Integer(), sql.ForeignKey("a.id"))
class Child2(base):
__tablename__ = 'c'
id = sql.Column(sql.Integer(), nullable=False, primary_key=True)
p_id = sql.Column(sql.Integer(), sql.ForeignKey("a.id"))
metadata.create_all()
db = DB()
c1 = Child1()
c2 = Child2()
p = Parent(id=1, child=c1, children=[c2])
db.add(p)
db.commit()
db.close()
db1 = DB()
p1 = db1.query(Parent).first()
print p1, p1.child, p1.children
print
p1.child = None
p1.children[:] = []
db1.close()
db2 = DB()
p2 = db2.merge(p1)
print p2, p2.child, p2.children
print
"""
I get this output:
<Parent 0x97a516c id=1> <Child1 0x979f26c id=1> [<Child2 0x97a530c
id=1>]
<Parent 0x97a576c id=1> <Child1 0x97a5d0c id=1> []
that is, when uselist=False, setting the attribute to None does not
persist across the merge.
Thanks,
a.
--
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.