Hello,
today I've been bit again by a one-to-many relation not updating when
its underlying foreign key changed. This feels inconsistent, especially
since its behavior depends on whether the relationship was referenced
before the change or not.
A reduced example is attached, with comments / expectations inline.
I was not able to find documentation, a bug or mailing list thread.
Maybe I missed something obvious?
Cheers
Robert
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
DBSession = scoped_session(sessionmaker())
engine = create_engine('sqlite://')
Base = declarative_base()
Base.metadata.bind = engine
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True)
content = Column(Integer)
user_id = Column(Integer, ForeignKey(User.id))
user = relationship("User")
Base.metadata.create_all()
user1 = User(id=23)
user2 = User(id=42)
DBSession.add(user2)
DBSession.add(user1)
article = Article()
article.user_id = user1.id
DBSession.add(article)
DBSession.flush()
print article.user.id # prints 23
print article.user_id # prints 23
article.user_id = 42
DBSession.flush()
print article.user.id # prints 42
print article.user_id # prints 23 !
# when we remove the "print article.user_id" above,
# this will print 42 though
DBSession.commit()
print article.user.id # prints 42
print article.user_id # prints 42