theres multiple levels of issues with this.

one is, its not very clean to move objects between databases using  
sessions.   to do so, you have to remove the "_instance_key" of the  
object and save it into the other session:

f = Foo()
sess1.save(f)
sess1.flush()

sess1.expunge(f)
del f._instance_key
sess2.save(f)
sess2.flush()

the second is, a mapper does not define *where* you are storing your  
object, it only defines *how*.  therefore you *never* make a second  
mapper for a class, unless you are using one of two very specific  
recipes which are mentioned in the docs (which this example is not).

third, the SessionContextExt shouldnt really "interfere" with this  
operation, in that it wont prevent you from expunging the object from  
one session and saving it into another, but it does make it  
confusing.  SessionContextExt is just the tiniest little convenience  
feature, that of "your objects automatically get saved into a  
session" and also "lazy loaders know how to find a session".  but if  
you are moving objects between sessions i would think its just going  
to confuse matters since its making decisions for you behind the  
scenes.  i think its important to try to make your code work while  
using the minimal (minimal here meaning, "none") number of "add-ons"  
to start with, so that you have something which works and can be  
understood.  then the add-ons can be implemented afterwards, as the  
need for them arises.

On Jul 24, 2007, at 10:18 AM, alex.schenkman wrote:

>
> Hi:
>
> I'm new writing web apps and I'm using cherrypy with sqlalchemy.
> As I understand it, each user navigating the site and clicking on
> pages gets a new thread and thus it is necesary to  use sqlalchemy in
> a thread-safe manner.
>
> After reading the docs I assume that I have to use SessionContextExt,
> but I don't figure out how.
>
> As a test I try to write records to two different databases, but I
> always get  a mapper error.
>
> sqlalchemy.exceptions.ArgumentError: Class '<class '__main__.Doc'>'
> already has a primary mapper defined with entity name 'None'.
>
> Any hint is much appreciated!!
>
>
> This is the code I try:
>
> from sqlalchemy import *
> from sqlalchemy.ext.sessioncontext import SessionContext
>
> class Doc(object):
>     def __init__(self, id, path, state):
>         self.DocID = id
>         self.Path = path
>         self.Complete = state
>
> if __name__ == "__main__":
>     db1 = create_engine( 'sqlite:///test.db' )
>     db1.echo = False
>     metadata = BoundMetaData( db1 )
>     docs = Table('docs', metadata, autoload=True)
>
>
>     ctx1 = SessionContext(create_session)
>     mapper(Doc, docs, extension=ctx1.mapper_extension)
>     d = Doc(43,'/etc/password',True)
>     ctx1.current.flush()
>
>
>     db2 = create_engine( 'sqlite:///test2.db' )
>     db2.echo = False
>     metadata2 = BoundMetaData( db2 )
>
>     d = Doc(15,'/etc/init',False)
>
>     ctx2 = SessionContext(create_session)
>     mapper(Doc, docs, extension=ctx2.mapper_extension)
>     ctx2.current.flush()
>
>
> >


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

Reply via email to