Bao Niu <niuba...@gmail.com> wrote:

> I learned that you cannot add the same model object to different session
> objects(unless you use scoped_session of course). However some behaviour
> seems strange and I don't know what I did wrong. Here is what I did:
> 
> from sqlalchemy import Column, String, Integer, ForeignKey from
> sqlalchemy.ext.declarative import declarative_base
> 
> Base = declarative_base()
> 
> class User(Base): __tablename__ = 'user' id = Column(Integer,
> primary_key=True) name = Column(String)
> 
> from sqlalchemy import create_engine engine = create_engine('sqlite:///')
> 
> from sqlalchemy.orm import sessionmaker session = sessionmaker()
> session.configure(bind=engine) Base.metadata.create_all(engine)
> 
> # Construct the first session object s1 = session() # Construct the second
> session object s2 = session() john = User(name='John') s1.add(john)
> s1.commit() s2.add(john) Traceback (most recent call last): File
> "<stdin>", line 1, in <module> File
> "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", line
> 1492, in add self._save_or_update_state(state) File
> "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", line
> 1504, in _save_or_update_state self._save_or_update_impl(state) File
> "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", line
> 1761, in _save_or_update_impl self._update_impl(state) File
> "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", line
> 1755, in _update_impl self._attach(state) File
> "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/session.py", line
> 1849, in _attach state.session_id, self.hash_key))
> sqlalchemy.exc.InvalidRequestError: Object '<User at 0x7f904ac63898>’ is
> already attached to session '1' (this is '2') >>> s1 is s2 False >>>
> s2.identity_map {(<class '__main__.User'>, (1,)):
> <sqlalchemy.orm.state.InstanceState object at 0x7f904ac63908>}
> 
> if s2.add(john) failed, I would expect s2 to be just as if nothing had
> been added to it. Why is s2 not empty??

this is a bug.
https://bitbucket.org/zzzeek/sqlalchemy/issue/3301/session_update_impl-adds-the-item-to
is added.

> ( I also attached a picture to illustrate what happened.) Thanks.
> 
> -- You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group. To unsubscribe from this group and stop
> receiving emails from it, send an email to
> sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email
> to sqlalchemy@googlegroups.com. Visit this group at
> http://groups.google.com/group/sqlalchemy. For more options, visit
> https://groups.google.com/d/optout.
> <wierd_session_behavior_2015-2-2_2:56:9.png>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to