On Feb 19, 2014, at 10:01 AM, Bao Niu <[email protected]> wrote: > 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? >
no because the target instance a product of either a. essentially loaded by session.query(User).get(1), so not expired or b. primary key identity 1 doesn’t exist in the database yet in which case the new object is only pending. > 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. > attributes that are not specified in the source object are not copied. this allows one to set a subset of attributes on the target object. > On Feb 19, 2014 6:47 AM, "Michael Bayer" <[email protected]> wrote: > > On Feb 19, 2014, at 6:08 AM, Bao Niu <[email protected]> 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/groups/opt_out.
signature.asc
Description: Message signed with OpenPGP using GPGMail
