user = self.user_query.get(1)
Message(origin=user.node, text='abcd')


InvalidRequestError: Object '<culture.model.Message
object at 0x4765490c>' is already attached to session
'1197808844' (this is '1082348588')

When you create Message, it is automatically associated with the current thread's session (as a result of the threadlocal mod). When you then attach the "user.node" object to its "origin" property, the Message mapper wants to "cascade" its session association to its child objects, which then produces an error since "user.node" is already associated with some other session, presumably because it was loaded in some other thread since the Query that loaded it is an instance variable.

this cascading behavior can be configured to behave differently, but its likely that you would want connected objects to all be within the same session.

so when you query for the user object, that query should most likely be done in the context of the current thread's session, which when using the "threadlocal" mod as you are doing is accessed via the "objectstore" object:

        user = objectstore.query(User).get(1)
        Message(origin=user.node, text='abcd')


On May 31, 2006, at 7:21 AM, Anton Krasovsky wrote:

Hi,

I'm using SQLAlchemy 0.2.1 with Turbogears,
and I see an error when trying to create an instance
of a mapped object.

Environment is as follows:

sqlalchemy is imported with:

    import sqlalchemy.mods.threadlocal
    import sqlalchemy

module 'model' defines number of Table's objects and
their mappings, in particular:

node_table = Table( "node", __engine__,
    Column( 'node_id', Integer, primary_key=True ),
    )

message_table = Table( "message", __engine__,
    Column( 'message_id', Integer, primary_key=True ),
    Column('text', String(255), nullable=False),
    Column('node_id', Integer,
ForeignKey('node.node_id')),
    Column('created', DateTime),
    )

class Node(object):
    pass

class User(object):
    def __init__(self, node, email, password):
        self.email = email
        self.password = password
        self.node = node

class Message(object):
    def __init__(self, origin, text,
created=datetime.now()):
        self.origin = origin
        self.text = text
        self.created = created

mapper(Node, node_table)

mapper(User, user_table, properties = {
    'node' : relation(Node)
    }
)

mapper(Message, message_table, properties = {
    'origin' : relation(Node, backref='messages')
    }
)

When turbogears starts, it imports this module (and
presumably also starts several threads)

I'm trying to create instance of Message from within
of one thread, with:

user = self.user_query.get(1)
Message(origin=user.node, text='abcd')

And I get following exception:

......
  File "/home/anton/culture/culture/controllers.py",
line 91, in submit
    Message(origin=user.node, text=body)
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/mapper.py",
line 398, in init
  File "/home/anton/culture/culture/model.py", line
70, in __init__
    self.origin = origin
  File
"build/bdist.linux-i686/egg/sqlalchemy/attributes.py",
line 56, in __set__
  File
"build/bdist.linux-i686/egg/sqlalchemy/attributes.py",
line 373, in set_attribute
  File
"build/bdist.linux-i686/egg/sqlalchemy/attributes.py",
line 140, in setattr
  File
"build/bdist.linux-i686/egg/sqlalchemy/attributes.py",
line 322, in set
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/unitofwork.py",
line 57, in append
  File
"build/bdist.linux-i686/egg/sqlalchemy/util.py", line
387, in append
  File
"build/bdist.linux-i686/egg/sqlalchemy/util.py", line
295, in __setrecord
  File
"build/bdist.linux-i686/egg/sqlalchemy/attributes.py",
line 217, in do_value_appended
  File
"build/bdist.linux-i686/egg/sqlalchemy/attributes.py",
line 92, in value_changed
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/unitofwork.py",
line 52, in do_value_changed
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/session.py",
line 300, in save_or_update
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/session.py",
line 339, in _save_impl
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/session.py",
line 357, in _register_new
  File
"build/bdist.linux-i686/egg/sqlalchemy/orm/session.py",
line 374, in _attach
InvalidRequestError: Object '<culture.model.Message
object at 0x4765490c>' is already attached to session
'1197808844' (this is '1082348588')

However I'm able to create instance of Message in
similar way, when not using threads.

Please advise if I'm doing something wrong here,
as I'm new to SQLAlchemy.

Regards,
Anton Krasovsky

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk! Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel? cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to