Re: [sqlalchemy] Re: object_session returns None, but state still has session_id
Hi Mike, It is an edge case for sure. I'm using SA with eventlet so the session is going out of scope in a green thread. Eventlet does some monkeying (literally) around with threadlocals so I get a different session for each one. However in this case the parent instance to which I'm adding obj was fetched in a one greenthread and the child is added in another after a timer has fired off (the child is a record of the timer running). It's definitely fair to say that I'm not using SA in an optimal way, however I'd still be inclined to say that the patch could be worthwhile here. I should also have mentioned that the original session would have been from a query_property rather than an explicitly created Session(). Is there any particular reason that you added the check where you did in the patch as opposed to the except KeyError clause in _state_session? Cheers, Ben -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/HfGgOzhCMwIJ. To post to this group, send email to sqlalchemy@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.
[sqlalchemy] object_session returns None, but state still has session_id
Hi Folks, I have the following code: obj = some object created by adding to a parent's collection ses = object_session(obj) if not ses: ses = Session() ses.add(obj) ses.commit() This is giving me InvalidRequestError: saying that the object is attached to a session. When looking into this I have found: - obj._sa_instance_state.session_id is not in sqlalchemy.orm.session._sessions. - object_session(parent) also returns None - The session_id given in the InvalidRequestError (i.e session_id in this part of the message is already attached to session 'session_id') == parent._sa_instance_state.session_id I'm not that familiar with SA's code, but wondered if it would be possible to check for a Session not being valid? Perhaps when the KeyError is caught in the sqlalchemy.orm.session._state_session function (line 1722 in my version) the state session_id could be set to None - or any other action taken that would make the state look like it didn't have a Session. As for a fix, I guess the best thing to do would be to make_transient(obj) or is there a better way? Thanks, Ben -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/bGDqyB64kZYJ. To post to this group, send email to sqlalchemy@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.
[sqlalchemy] Re: object_session returns None, but state still has session_id
I've now tried a make_transient so my code looks like this: obj = some object created by adding to a parent's collection ses = object_session(obj) if not ses: make_transient(obj) ses = Session() ses.add(obj) ses.commit() I still get the same exception. I guess my other option now is to do: state = attributes.instance_state(obj) state.detach() Would that work do you think? BTW would this be considered a bug, should I raise a bug report on this? Cheers, Ben -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/vnL1PJednqcJ. To post to this group, send email to sqlalchemy@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.