[sqlalchemy] in_ SADeprecationWarning
I use the following code: query = meta.Session.query(db.Object) all_objects = [1,2,3] objects = query.filter(db.Object.ObjectID.in_(*all_objects)).all() And I get this error: SADeprecationWarning: passing in_ arguments as varargs is deprecated, in_ takes a single argument that is a sequence or a selectable Am I doing something wrong? Because without the * objects = query.filter(db.Object.ObjectID.in_(all_objects)).all() It throws another error: ProgrammingError('(ProgrammingError) operator does not exist: bigint = integer[] LINE 3: WHERE Object.ObjectD = ARRAY[1, 3] HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: in_ SADeprecationWarning
SA version? sql dialect? what's table definition? On Thursday 03 July 2008 14:43:44 Marin wrote: I use the following code: query = meta.Session.query(db.Object) all_objects = [1,2,3] objects = query.filter(db.Object.ObjectID.in_(*all_objects)).all() And I get this error: SADeprecationWarning: passing in_ arguments as varargs is deprecated, in_ takes a single argument that is a sequence or a selectable Am I doing something wrong? Because without the * objects = query.filter(db.Object.ObjectID.in_(all_objects)).all() It throws another error: ProgrammingError('(ProgrammingError) operator does not exist: bigint = integer[] LINE 3: WHERE Object.ObjectD = ARRAY[1, 3] HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: in_ SADeprecationWarning
I got it... I wanted to replicate the error in a single file, but I couldn't have them booth. I realized then that the problem was in 2 different SA version installed on my computer. Now that i removed the older everything works correctly. On Jul 3, 1:48 pm, [EMAIL PROTECTED] wrote: SA version? sql dialect? what's table definition? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Polymorphic association woes
Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~--- poly_assoc_3.py Description: application/python
[sqlalchemy] Re: Polymorphic association woes
what that should do? An association to a polymorphic object that is not plain-class-hierarchy (which is, several associations, one per subtype)? i am asking as this seems as another way to do aspects/multiple inheritance; my way is a single association with multiple references (to the multiple possible types) inside it and type-discriminator. while your way is switching the association depending on type discriminator... or am i wrong? On Thursday 03 July 2008 16:31:54 Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] IDEA: Call-local vs thread-local context
Hi there, I have an idea I'd like to bounce off people here. It relates to the problem described here: http://www.sqlalchemy.org/docs/05/session.html#unitofwork_contextual The solution used there makes the assumption that each request will happen in its own thread, and uses python's thread-local stuff to do its magic. I was wondering if it is not possible to do something that is local to the current call-stack, instead of the current thread. I think there are two issues here: a) a general mechanism for remembering a call-local context b) using it to provide a call-local sqlalchemy session Now, I'm not sure yet how to do (b), but want to give an example of (a) with a naive implementation to illustrate the idea. (Just note this mechanism can be used for other important contextual info as well, such as who the current user is, etc) With the following code, I can call a method b.bar, passing it some context (which is not part of the method's original signature). b.bar calls a.foo in turn, also not passing it anything, but inside A.foo, the context can be retrieved from the call stack. The only overhead incurred is during this last step, and I don't think it is much. (Implementation of CC is at the bottom.) Any thoughts? class A(object): def foo(self): print CC.get_context()#CC here is assumed to be a class, on which a class method is # called to get the current context, which may be our session # but I think it can be used for more things than just the session class B(object): a = A() def bar(self, arg): print arg self.a.foo() b = B() CC(b.bar, 'context stuff - a session or a dict?', 'some argument for bar()') #- # CC (should be before the examples above if you want to run it) # import inspect class CC(object): def __init__(self, callable, context, *args, **kwargs): self.callable = callable self.context = context self.callable(*args, **kwargs) @classmethod def get_context(cls): context = None found = False f = inspect.currentframe() while not found and f.f_back: theSelf = f.f_locals.get('self', None) if theSelf.__class__ is cls: context = theSelf.context found = True nextF = f.f_back del f f = nextF return context --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: IDEA: Call-local vs thread-local context
On Jul 3, 2008, at 5:05 AM, Iwan wrote: I was wondering if it is not possible to do something that is local to the current call-stack, instead of the current thread. I think there are two issues here: a) a general mechanism for remembering a call-local context b) using it to provide a call-local sqlalchemy session Now, I'm not sure yet how to do (b), but want to give an example of (a) with a naive implementation to illustrate the idea. (Just note this mechanism can be used for other important contextual info as well, such as who the current user is, etc) With the following code, I can call a method b.bar, passing it some context (which is not part of the method's original signature). b.bar calls a.foo in turn, also not passing it anything, but inside A.foo, the context can be retrieved from the call stack. The only overhead incurred is during this last step, and I don't think it is much. (Implementation of CC is at the bottom.) Any thoughts? the currentframe usage is something I'd leave to the deeper Python folks to argue over its appropriateness. As far as SA's contextual session, it was always meant to allow any kind of context, not just threadlocal. Here, it would look like: Session = scoped_session(sessionmaker(), scopefunc=get_context) where get_context needs to return anything that is hashable, to key off to the Session inside of a dict. We have at least one user who used this function in the past to relate Sessions to graphical windows. So give it a try ! --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
the first set of operations on the Session place every new object, including two Address objects and two GenericAssoc objects, in the session. Then the session is cleared. Then, a series of loads load in all those same objects, including the same Address objects and GenericAssoc objects, except different instances of them local to that session (since the previous were cleared). Then, when u2 = User() is created and is associated with a2 from the *first* session, the two original GenericAssoc objects (which were cleared) now become associated with that User object via the connection to Address, which fail to be added since there are already two GenericAssoc's from the second Session usage present. On Jul 3, 2008, at 9:31 AM, Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. -- Gaëtan de Menten http://openhex.org poly_assoc_3.py --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Using domain object instead of Table as first argument of select function
Hi everybody, I am working on my graduate (final :)) exam, and SQLAlchemy is main topic. I was tryinig example from ORM tutorial: from sqlalchemy import select, func session.query(User).from_statement( ... select( ...[users_table], ... select([func.max(users_table.c.name)]).label('maxuser')==users_table.c.name) ...).all() [User('wendy','Wendy Williams', 'foobar')] And this looks too ugly for me, and not only that, I think that it is not O.K. to use table and columns in ORM (at least I should have an alternative way doing it without table and column objects). So I tried something this (in my example User is Client): session.query(Client).from_statement( ... select( ... [Client], ... select([func.max(Client.name)]).label('max_name') == Client.name) ... ).all() but, it is not working, the problem is in first argument of select. Then I tried Client.c as first argument, but without success. Only this works: session.query(Client).from_statement( ... select( ... Client.c._data.values(), ... select([func.max(Client.name)]).label('max_name') == Client.name) ... ).all() but it is so ugly hack. Is it some better way of doing this? Sorry for my bad English :) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Using domain object instead of Table as first argument of select function
On Jul 3, 2008, at 10:52 AM, [EMAIL PROTECTED] wrote: Is it some better way of doing this? keep reading the tutorial and then try sess.query(User).filter(User.name==select([func.max(User.name)])) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: IDEA: Call-local vs thread-local context
it is possible to dig the stack to find the context u need... no worries about that, but u have to know the name of it, as there might be two contexts living in same scope (e.g.: copy data from DB1 to DB2), and u could get the wrong one -- python has no ordering in its namespaces/scopes/frames (a plain dict). There's another approach, organise yourself a stack and use that one, be it threadlocal or global or what. i have some working example at dbcook/misc/timed/util/stacks.py: http://dbcook.svn.sourceforge.net/viewvc/dbcook/trunk/dbcook/misc/timed/util/ and a maybe-working usage in dbcook/misc/timed/ timecontext.py + setup.py have a look svilen On Thursday 03 July 2008 12:05:37 Iwan wrote: Hi there, I have an idea I'd like to bounce off people here. It relates to the problem described here: http://www.sqlalchemy.org/docs/05/session.html#unitofwork_contextua l The solution used there makes the assumption that each request will happen in its own thread, and uses python's thread-local stuff to do its magic. I was wondering if it is not possible to do something that is local to the current call-stack, instead of the current thread. I think there are two issues here: a) a general mechanism for remembering a call-local context b) using it to provide a call-local sqlalchemy session Now, I'm not sure yet how to do (b), but want to give an example of (a) with a naive implementation to illustrate the idea. (Just note this mechanism can be used for other important contextual info as well, such as who the current user is, etc) With the following code, I can call a method b.bar, passing it some context (which is not part of the method's original signature). b.bar calls a.foo in turn, also not passing it anything, but inside A.foo, the context can be retrieved from the call stack. The only overhead incurred is during this last step, and I don't think it is much. (Implementation of CC is at the bottom.) Any thoughts? class A(object): def foo(self): print CC.get_context()#CC here is assumed to be a class, on which a class method is # called to get the current context, which may be our session # but I think it can be used for more things than just the session class B(object): a = A() def bar(self, arg): print arg self.a.foo() b = B() CC(b.bar, 'context stuff - a session or a dict?', 'some argument for bar()') #- # CC (should be before the examples above if you want to run it) # import inspect class CC(object): def __init__(self, callable, context, *args, **kwargs): self.callable = callable self.context = context self.callable(*args, **kwargs) @classmethod def get_context(cls): context = None found = False f = inspect.currentframe() while not found and f.f_back: theSelf = f.f_locals.get('self', None) if theSelf.__class__ is cls: context = theSelf.context found = True nextF = f.f_back del f f = nextF return context --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
On Thursday 03 July 2008 17:15:40 Michael Bayer wrote: the first set of operations on the Session place every new object, including two Address objects and two GenericAssoc objects, in the session. Then the session is cleared. Then, a series of loads load in all those same objects, including the same Address objects and GenericAssoc objects, except different instances of them local to that session (since the previous were cleared). Then, when u2 = User() is created and is associated with a2 from the *first* session, the two original GenericAssoc objects (which were cleared) now become associated with that User object via the connection to Address, which fail to be added since there are already two GenericAssoc's from the second Session usage present. ahh yes i had this same a week ago but i forgot. so is u2=session.merge(u2) a cure? i used it on some similar (test) case On Jul 3, 2008, at 9:31 AM, Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. -- Gaëtan de Menten http://openhex.org poly_assoc_3.py --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
On Thu, Jul 3, 2008 at 6:01 PM, [EMAIL PROTECTED] wrote: On Thursday 03 July 2008 17:15:40 Michael Bayer wrote: the first set of operations on the Session place every new object, including two Address objects and two GenericAssoc objects, in the session. Then the session is cleared. Then, a series of loads load in all those same objects, including the same Address objects and GenericAssoc objects, except different instances of them local to that session (since the previous were cleared). Then, when u2 = User() is created and is associated with a2 from the *first* session, the two original GenericAssoc objects (which were cleared) now become associated with that User object via the connection to Address, which fail to be added since there are already two GenericAssoc's from the second Session usage present. Jeeez. I hate myself for thinking it was some complex thing related to the pattern used and not even looking further... ahh yes i had this same a week ago but i forgot. so is u2=session.merge(u2) a cure? i used it on some similar (test) case a2 = sess.merge(a2) is a cure indeed. On Jul 3, 2008, at 9:31 AM, Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
so my woe about this thing which i asked in the aspects-revisited mail: can i get all someaddress.members in ONE query? some secondary mapper - but how? polymorphic-union? On Thursday 03 July 2008 19:36:12 Gaetan de Menten wrote: On Thu, Jul 3, 2008 at 6:01 PM, [EMAIL PROTECTED] wrote: On Thursday 03 July 2008 17:15:40 Michael Bayer wrote: the first set of operations on the Session place every new object, including two Address objects and two GenericAssoc objects, in the session. Then the session is cleared. Then, a series of loads load in all those same objects, including the same Address objects and GenericAssoc objects, except different instances of them local to that session (since the previous were cleared). Then, when u2 = User() is created and is associated with a2 from the *first* session, the two original GenericAssoc objects (which were cleared) now become associated with that User object via the connection to Address, which fail to be added since there are already two GenericAssoc's from the second Session usage present. Jeeez. I hate myself for thinking it was some complex thing related to the pattern used and not even looking further... ahh yes i had this same a week ago but i forgot. so is u2=session.merge(u2) a cure? i used it on some similar (test) case a2 = sess.merge(a2) is a cure indeed. On Jul 3, 2008, at 9:31 AM, Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
also, can someone explain what is the difference (sql-plan-wise) in the approaches? this one is: address - addressable - users - orders my one is otherway around: address - addressable - users - orders both declare multiple (per-type) relations/backrefs on the intermediate adressable - just 1-to-many vs many-to-1. this puts foregnkeys into the ends, why my one puts foregnkeys on the adressable. On Thursday 03 July 2008 19:36:12 Gaetan de Menten wrote: On Thu, Jul 3, 2008 at 6:01 PM, [EMAIL PROTECTED] wrote: On Thursday 03 July 2008 17:15:40 Michael Bayer wrote: the first set of operations on the Session place every new object, including two Address objects and two GenericAssoc objects, in the session. Then the session is cleared. Then, a series of loads load in all those same objects, including the same Address objects and GenericAssoc objects, except different instances of them local to that session (since the previous were cleared). Then, when u2 = User() is created and is associated with a2 from the *first* session, the two original GenericAssoc objects (which were cleared) now become associated with that User object via the connection to Address, which fail to be added since there are already two GenericAssoc's from the second Session usage present. Jeeez. I hate myself for thinking it was some complex thing related to the pattern used and not even looking further... ahh yes i had this same a week ago but i forgot. so is u2=session.merge(u2) a cure? i used it on some similar (test) case a2 = sess.merge(a2) is a cure indeed. On Jul 3, 2008, at 9:31 AM, Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
seems this way many users can have same adress, but one user (or order) cannot have more than one adress. so it's a polymorphic 1-to-many. my one is for a many to many. correct? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: transactions with multiple potential constraint violations
On Jul 3, 2008, at 1:09 PM, Simon wrote: Hi all, I have a transactional session which save()s multiple objects to multiple tables where each object potentially violates a unique primary key or just a unique key constraint. At flush() time, MySQL returns an IntegrityError which specifies the value of the offending key and the key's number within its table (i.e. first key, second key...), but I cannot from this information figure out which of my objects is the culprit. Any suggestions on how to solve this problem? Ideally, I would like to rollback() the whole transaction when such an error occurs, regenerate the keys on the offending object, and repeat until commit() succeeds. use SQLA 0.5 if you'd like to rollback() a session and continue using the objects within. Read through http://www.sqlalchemy.org/docs/05/session.html#unitofwork_using_rolling and elsewhere to see what the SQLA contract is regarding sessions, errors and rolling back. It's also worthwhile to give SAVEPOINT a try, which allows partial transactions to commit and is described at http://www.sqlalchemy.org/docs/05/session.html#unitofwork_managing_savepoint . In particular, some databases such as Postgres do not allow the transaction to continue if an integrity error is raised, necessitating the use of SAVEPOINT, or just querying the table beforehand (which is the traditional approach to this problem). --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Polymorphic association woes
in this example, if u make another user u2 pointing to same address1, u1.adresses goes wrong... - last one wins. On Thursday 03 July 2008 19:36:12 Gaetan de Menten wrote: On Thu, Jul 3, 2008 at 6:01 PM, [EMAIL PROTECTED] wrote: On Thursday 03 July 2008 17:15:40 Michael Bayer wrote: the first set of operations on the Session place every new object, including two Address objects and two GenericAssoc objects, in the session. Then the session is cleared. Then, a series of loads load in all those same objects, including the same Address objects and GenericAssoc objects, except different instances of them local to that session (since the previous were cleared). Then, when u2 = User() is created and is associated with a2 from the *first* session, the two original GenericAssoc objects (which were cleared) now become associated with that User object via the connection to Address, which fail to be added since there are already two GenericAssoc's from the second Session usage present. Jeeez. I hate myself for thinking it was some complex thing related to the pattern used and not even looking further... ahh yes i had this same a week ago but i forgot. so is u2=session.merge(u2) a cure? i used it on some similar (test) case a2 = sess.merge(a2) is a cure indeed. On Jul 3, 2008, at 9:31 AM, Gaetan de Menten wrote: Hi list, Could anybody tell me what's wrong with the following code? It's only the code in the poly_assoc example directory (the generic version) where I'm trying to set the polymorphic part of the relationship. It gets me the traceback below and I don't really understand why: Traceback (most recent call last): File poly_assoc_3.py, line 155, in module sess.save(u2) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/util.py, line 1550, in func_with_warning return func(*args, **kwargs) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1055, in save self._cascade_save_or_update(state, entity_name) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line , in _cascade_save_or_update self._save_or_update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1251, in _save_or_update_impl self._update_impl(state) File /home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/orm/ session.py, line 1241, in _update_impl session. % (mapperutil.state_str(state), state.key)) sqlalchemy.exc.InvalidRequestError: Could not update instance '[EMAIL PROTECTED]', identity key (class '__main__.GenericAssoc', (1,), None); a different instance with the same identity key already exists in this session. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: sharding id_chooser query_chooser
For example, you have user, post, comment table. Sharding is done by user_id and shard_lookup is done via lookup table. If one is going to create a post, you would lookup the user_id in the lookup table and insert the post entry into the shard where the user_id belongs to. How would you translate this type of behaviour in sqlalchemy. eg. shardN - table1, ..., tableM #each tables have field called username, and username field from user table is not a foreign key to post or comment table. There are no foreign keys. shard1 - user, post, comment shard2 - user, post, comment lookup_table has the following field: username shardname Now, if you want to create and save a post entry, you would look it up in lookup_table with the username to find the shardname and save it on that shard. Since shard lookup is done based on username for all three tables (user, post, comment), you can have one common function, def shard_chooser(mapper, instance, clause=None), for all three tables. What is the case for the other ShardSession related functions for the user, post, comment tables, would each table would have their own verison of def id_chooser(query, ident) def query_chooser(query) ? And also not sure where and how you would associate lookup table, username and shardname mapping, into these functions. On Jun 27, 6:06 am, King Simon-NFHD78 [EMAIL PROTECTED] wrote: Lilo wrote: My understanding of this query_chooser is that it's used when you want to execute orm's sql rather than raw sql. I don't quite understand what is visit_binary function do from attribute_shard.py example. What does it mean binary.operator, binary.left, binary.right.clause and query._criterion? The sharding design behind our application is that we have a master lookup table and shards. What shard to execute sql is based on querying master lookup table. taken from sqlalchemy attribute_shard.py example: def query_chooser(query): ids = [] # here we will traverse through the query's criterion, searching # for SQL constructs. we'll grab continent names as we find them # and convert to shard ids class FindContinent(sql.ClauseVisitor): def visit_binary(self, binary): if binary.left is weather_locations.c.continent: if binary.operator == operators.eq: ids.append(shard_lookup[binary.right.value]) elif binary.operator == operators.in_op: for bind in binary.right.clauses: ids.append(shard_lookup[bind.value]) FindContinent().traverse(query._criterion) if len(ids) == 0: return ['north_america', 'asia', 'europe', 'south_america'] else: return ids thank you. Hi, (I'm probably going to get the details wrong here, but hopefully the general idea will be right) SQLAlchemy represents SQL expressions as objects, a bit like a parse tree. For example, there are classes that represent tables, joins, functions and so on. It uses a Visitor pattern (http://en.wikipedia.org/wiki/Visitor_pattern) to traverse these structures. A binary clause is an SQL expression with an operator, a left half and a right half. For example, in the clause 'id = 5', binary.left is 'id', binary.right is '5', and binary.operator is '=' (or rather, operators.eq, which is the object that represents '='). The query_chooser function above uses a Visitor to look through all the SQL expressions that make up the query that is about to be executed. Because the only overridden method is 'visit_binary', anything other than binary clauses are ignored. The method body could be written in long-hand as: If the left part of the expression is 'weather_locations.continent': If the expression is 'continent = XXX': add the shard for continent XXX Else if the expression is 'continent IN (XXX, YYY)': add the shards for XXX and YYY (operators.in_op corresponds to the 'IN' operator, and binary.right.clauses contains the right-hand-side of that expression) The fallback case (if len(ids) == 0) happens if the visitor failed to find any expressions that it could handle, in which case all the shards will be queried. I don't understand your situation well enough to know how to adapt the example. If your master lookup table is basically doing the same job as the shard_lookup dictionary in the example, then you could replace shard_lookup above with a function call that does the query. I hope that helps, Simon --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---