[sqlalchemy] Many to One vs session

2010-10-29 Thread Mark Erbaugh
I have a table (T) that has a many-to-one relationship (via foreign key 
inclusion) to a category table (C). Let's name the relationship category. When 
I retrieve an existing T record, SA populates the category field with an 
instance of C.  I can change the category to a different value by storing a 
different instance of C into category.

My question is does it matter if the various instances of C are associated with 
the same session as the T instance? Can the C instances come from a different 
session, or can the be expunge'd from the session that retrieved them?  Can I 
store an entirely new C instance, and if so, will SA do an insert into the C 
table?


Thanks,
Mark

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Many to One vs session

2010-10-29 Thread Conor
On 10/29/2010 01:23 PM, Mark Erbaugh wrote:
 I have a table (T) that has a many-to-one relationship (via foreign key 
 inclusion) to a category table (C). Let's name the relationship category. 
 When I retrieve an existing T record, SA populates the category field with an 
 instance of C.  I can change the category to a different value by storing a 
 different instance of C into category.

Technically, SQLAlchemy by default does not populate the category
relation until you access it the first time (i.e. it is a lazy load).

 My question is does it matter if the various instances of C are associated 
 with the same session as the T instance?

I'm not sure what you mean here. There is no problem having multiple C
instances in the same session as the T instance. SQLAlchemy will
complain (hopefully; I haven't tried it) if multiple C instances in the
same session share a primary key.

 Can the C instances come from a different session, or can the be expunge'd 
 from the session that retrieved them?

The only restriction is, if the T instance is part of session S, then
the C instance that you assign to T.category must be part of S or not be
part of any session. It is allowed if you first expunge the C instance
from another session and merge it into S before assigning it to T.category.

Note that there are only a few use cases for moving instances from one
session to another that I am aware of:

* Caching query results. You expunge the instances from the original
  session, keep them around in memory (or serialized to disk,
  memcache, etc.). Then you can merge them back into subsequent
  sessions to avoid repeated DB queries.
* Passing objects between threads. Sessions are not thread-safe, so
  if you want to pass objects from thread A to thread B, you have to
  merge session A's objects into session B before thread B can use them.

 Can I store an entirely new C instance, and if so, will SA do an insert into 
 the C table?

Yes, this is part of the save-update cascade[1] which is enabled by
default on a relationship. Generally you only worry about cascade for
one-to-many or many-to-many relationships. At least that's my experience.

-Conor

[1]
http://www.sqlalchemy.org/docs/orm/relationships.html#the-relationship-api

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Many to One vs session

2010-10-29 Thread Mark Erbaugh

On Oct 29, 2010, at 9:39 PM, Conor wrote:

 I have a table (T) that has a many-to-one relationship (via foreign key 
 inclusion) to a category table (C). Let's name the relationship category. 
 When I retrieve an existing T record, SA populates the category field with 
 an instance of C.  I can change the category to a different value by storing 
 a different instance of C into category.
 Technically, SQLAlchemy by default does not populate the category relation 
 until you access it the first time (i.e. it is a lazy load).
 
 My question is does it matter if the various instances of C are associated 
 with the same session as the T instance?
 I'm not sure what you mean here. There is no problem having multiple C 
 instances in the same session as the T instance. SQLAlchemy will complain 
 (hopefully; I haven't tried it) if multiple C instances in the same session 
 share a primary key.
 
 Can the C instances come from a different session, or can the be expunge'd 
 from the session that retrieved them?
 The only restriction is, if the T instance is part of session S, then the C 
 instance that you assign to T.category must be part of S or not be part of 
 any session. It is allowed if you first expunge the C instance from another 
 session and merge it into S before assigning it to T.category.
 

Thanks, that was what I was asking. I think I've tested it with C instances 
belonging to the same session and to C instances that have been expunged, but I 
didn't test C instances that were still an active part of a different session.


 Note that there are only a few use cases for moving instances from one 
 session to another that I am aware of:
 
 Caching query results. You expunge the instances from the original session, 
 keep them around in memory (or serialized to disk, memcache, etc.). Then you 
 can merge them back into subsequent sessions to avoid repeated DB queries.
 Passing objects between threads. Sessions are not thread-safe, so if you want 
 to pass objects from thread A to thread B, you have to merge session A's 
 objects into session B before thread B can use them.
 Can I store an entirely new C instance, and if so, will SA do an insert into 
 the C table?
 Yes, this is part of the save-update cascade[1] which is enabled by default 
 on a relationship. Generally you only worry about cascade for one-to-many or 
 many-to-many relationships. At least that's my experience.
 

Mark

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.