A persistent object doesn't populate an unloaded backref on a forward set event. This is for efficiency so that when you do something like:
for b in Session.query(B): b.a = some_a it doesn't spend time loading the "bs" collection of "some_a", which if you had a lot of different "some_a" would take a lot of time. The other direction: for a in Session.query(A): a.bs.append(some_b) if you were to access "some_b.a", the lookup is from the identity map since some_b is present. There is a step that ensures that the "change" from the backref is present in the "history" of the other side, but this is done in such a way as to not force a collection or reference load. I frequently forget the details of behaviors like these since 90% of them have been nailed down years ago, so if you try the following test case, you'll see no SQL is emitted after "2.----". Also I have to run out so I may be forgetting some other details about this, I'll try to take a second look later. from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) bs = relationship("B", backref="a") class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True) a_id = Column(Integer, ForeignKey('a.id')) e = create_engine('sqlite://', echo=True) Base.metadata.create_all(e) s = Session(e) s.add(A(id=1, bs=[B(id=1), B(id=2)])) s.commit() s.close() a = A(id=1, bs=[B(id=1), B(id=2)]) print "1. -----------------------------------------" a2 = s.merge(a) print "2. -----------------------------------------" for b in a2.bs: assert b.a is a2 On Jul 6, 2011, at 4:24 PM, Kent wrote: > If I merge() an object with a collection property, the backref's are > not set as they would be if I had assigned the collection to the > object. > > I expected that this should occur. Is there rationale for not setting > backref's or would it be possible to make this change? > > Thanks, > Kent > > -- > 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 > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- 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 sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.