On Thu, Aug 13, 2015 at 2:03 AM, Jinghui Niu <niujing...@gmail.com> wrote:
> About the Session.merge(), the documentation gives several examples of use
> cases, I think there might be a slight ambiguity in this one:
>
>> An application which reads an object structure from a file and wishes to
>> save it to the database might parse the file, build up the structure, and
>> then use merge() to save it to the database, ensuring that the data within
>> the file is used to formulate the primary key of each element of the
>> structure. Later, when the file has changed, the same process can be re-run,
>> producing a slightly different object structure, which can then be merged in
>> again, and the Session will automatically update the database to reflect
>> those changes, loading each object from the database by primary key and then
>> updating its state with the new state given.
>
>
>
> In this case when "the same process re-run, producing a slightly different
> object structure," does this newer object carry a primary key? If it's just
> been produced by parsing from the file, then I think probably it doesn't
> have a primary key. If it doesn't have a primary key, then merge() will
> create an entirely new entry in database for it instead of updating the
> older one, right? And this would defeat the updating intention, right?
>
> I think I probably missed some point here. Could someone elaborate it a
> little bit? Thanks.
>

I think the bit you missed is:

>> ensuring that the data within the file is used to formulate
>> the primary key of each element of the structure

ie. for this to work, the file must contain enough information to
construct the primary key for each object. I don't *think* it even
makes sense to use Session.merge() with an object that doesn't already
have a primary key, as the primary key dictates which existing
database rows you are trying to merge with.

Hope that helps,

Simon

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to