Re: [sqlalchemy] Copying Between Databases - Can I modify part of a Composite Key?

2012-09-27 Thread Shawn Wheatley
the most idiomatic way to handle this is to merge the objects in: obj = session.merge(existing_object) this will emit a SELECT for the existing row, then copy the state of existing_object to an object located for that primary key, if found. It ensures that the correct choice of

Re: [sqlalchemy] Copying Between Databases - Can I modify part of a Composite Key?

2012-09-27 Thread Shawn Wheatley
On Thursday, September 27, 2012 9:21:57 AM UTC-4, Shawn Wheatley wrote: the most idiomatic way to handle this is to merge the objects in: obj = session.merge(existing_object) this will emit a SELECT for the existing row, then copy the state of existing_object to an object located

Re: [sqlalchemy] Copying Between Databases - Can I modify part of a Composite Key?

2012-09-27 Thread Simon King
On Thu, Sep 27, 2012 at 2:34 PM, Shawn Wheatley swheat...@gmail.com wrote: On Thursday, September 27, 2012 9:21:57 AM UTC-4, Shawn Wheatley wrote: the most idiomatic way to handle this is to merge the objects in: obj = session.merge(existing_object) this will emit a SELECT for the

Re: [sqlalchemy] Copying Between Databases - Can I modify part of a Composite Key?

2012-09-27 Thread Shawn Wheatley
session.merge only looks at the primary key of the instance you are inserting (it pays no attention to unique constraints). In your example, the table contains a single row with PK (1, 1), and you are merging an instance with PK (1, 4). SA sees these as different, so it tries to INSERT

Re: [sqlalchemy] Copying Between Databases - Can I modify part of a Composite Key?

2012-09-27 Thread Shawn Wheatley
Clarification, I can't make changes to the underlying *database* (i.e. change the PK) but I can change my SA mapping. I did make the mapping change and it seems to run like a champ now, updating the env_id before either inserting or updating the row. Thanks everyone for your help! Shawn On

Re: [sqlalchemy] Copying Between Databases - Can I modify part of a Composite Key?

2012-09-27 Thread Michael Bayer
To answer the actual subject of the thread, SQLAlchemy will also update the PK itself if you do in fact change the primary key on the instance as loaded. but you'd have to roll a pseduo-merge like function to do this: old_object = session.query(Class).get((acct_id, env_id)) old_object.env_id