[sqlalchemy] Re: SQLAlchemy ORM Object caching with relationships and invalidation

2012-09-27 Thread David McKeone
Hi Mike, I'll let the others add more detail about your questions, but for the broad strokes I thought I'd add that I ran into similar issues with my simple caching method and ultimately ended up using the new Dogpile.cache stuff that Mike recommended on his

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

[sqlalchemy] [Q] Why the following emits SQL for slot access?

2012-09-27 Thread Ladislav Lenart
Hello again. I have the following test query: def test_slot_access_after_query(self): q = self.session.query(Foo, Bar).with_labels() q = q.filter(Foo.bar_id == Bar.id) rows = q.all() assert len(rows) 0 with self.assert_no_sql_while(): for

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

Re: [sqlalchemy] [Q] Why the following emits SQL for slot access?

2012-09-27 Thread Michael Bayer
On Sep 27, 2012, at 1:55 PM, Ladislav Lenart wrote: Hello again. I have the following test query: def test_slot_access_after_query(self): q = self.session.query(Foo, Bar).with_labels() q = q.filter(Foo.bar_id == Bar.id) rows = q.all() assert len(rows) 0

Re: [sqlalchemy] [Q] Why the following emits SQL for slot access?

2012-09-27 Thread Ladislav Lenart
Hello. not sure, is each.bar a typo and you meant _each_bar ? No, I mean each.bar. or are you trying to fetch a relationship along Foo- Bar ? if its a relationship and the linkage is not a simple many-to-one, then it emits SQL. Yes, I am trying to fetch Foo - Bar relationship. It is a

Re: [sqlalchemy] [Q] Why the following emits SQL for slot access?

2012-09-27 Thread Ladislav Lenart
Update. Te unwrap version works only if the original result of q.all() is kept around: def test_access(self): q = self.session.query(Foo, Bar).with_labels() q = q.filter(Foo.bar_id == Bar.id) # Only this combination works: _rows = q.all() rows =

Re: [sqlalchemy] [Q] Why the following emits SQL for slot access?

2012-09-27 Thread Ladislav Lenart
Hello. Thank you for the explanation. I was relatively close :-) I understand the rationale but don't like the impact it has: If I query the database for something, I expect it to be available via slot access afterwards, but unless I hold onto the results, it won't be. I guess this is one of

Re: [sqlalchemy] [Q] Why the following emits SQL for slot access?

2012-09-27 Thread Michael Bayer
On Sep 27, 2012, at 4:37 PM, Ladislav Lenart wrote: Hello. Thank you for the explanation. I was relatively close :-) I understand the rationale but don't like the impact it has: If I query the database for something, I expect it to be available via slot access afterwards, but unless I

[sqlalchemy] query joining and filtering with table inheritance

2012-09-27 Thread Iain Duncan
First off, sorry if it turns out that what I'm trying to do is smoke crack here, I'm by no means a DB or SQLA expert. =) I have two classes, Person and Organization, that use table inheritance to inherit from Contact. ( Person.id is an fkey to Contact.id ), which is working fine. I want to