class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
addresses = relationship("Address", backref="user")
>>> u = User(id=1)
>>> session.merge(u)
>>> session.commit()
>>> session.add(u)
>>> session.commit()
Thanks for replying. Isn't it the case when I just provide a primary key for the source object, the attribute shoul be marked expired on the target instance? In my example I only give the object u a primary key value, so the newly created object should be an object that only has a primary key value and "wipe out" other attributes originally for that row in the table. However the result is that row stays unchanged after all my instructions. Is there something I missed here?
Thanks for replying. Isn't it the case when I just provide a primary key for the source object, the attribute shoul be marked expired on the target instance?
In my example I only give the object u a primary key value, so the newly created object should be an object that only has a primary key value and "wipe out" other attributes originally for that row in the table.
On Feb 19, 2014 6:47 AM, "Michael Bayer" <mik...@zzzcomputing.com> wrote:On Feb 19, 2014, at 6:08 AM, Bao Niu <niub...@gmail.com> wrote:I thought I understood the behaviour of session.merge() from reading the documentation, but I'm wrong. Here is an example. I have codes below,class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
addresses = relationship("Address", backref="user")This class has a corresponding table 'user' which has already been populated with one row, with its primary key equal to 1.Now I run the following instructions from python -i:>>> u = User(id=1)
>>> session.merge(u)
>>> session.commit()I expected that there should be a sqlalchemy.orm.exc.FlushError, because both object u (which has been merged) and the row which has been already in the table have their primary key equal to 1. However, nothing happened. Sqlalchemy just ignored that merged u!If I do these instructions:>>> session.add(u)
>>> session.commit()Then I got a sqlalchemy.orm.exc.FlushError, which was expected.So, session.merge() does not include .add()? From the documentation it seems that it does.So first is that the object you pass to merge() is *never* itself added to the session. The object you pass in is unmodified. The return value of merge() is the object that is actually in the Session. This object will either have been newly created, in which case it was added using add(), or it was loaded from the database. But that object is never the one you passed in.So in your test if primary key “1” already exists, then merge() returned the User object it had for that primary key.
--
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 sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.