[Sqlalchemy-tickets] Issue #4310: the behavior about session commit is unexpected in ORM!!! (zzzeek
Brought to you by:
zzzeek
From: Li G. <iss...@bi...> - 2018-08-01 14:48:43
|
New issue 4310: the behavior about session commit is unexpected in ORM!!! https://bitbucket.org/zzzeek/sqlalchemy/issues/4310/the-behavior-about-session-commit-is Li GuangTian: ``` #!python from sqlalchemy import Column, Integer, String from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(16)) password = Column(String(16)) def __repr__(self): return "<User(name='%s', password='%s')>" % ( self.name, self.password) if __name__ == '__main__': engine = create_engine( 'mysql+pymysql://', echo=False) Session = sessionmaker(bind=engine) session = Session() user = User(name="test", password="test") print(len(user.__dict__.keys())) # 3 session.add(user) print(len(user.__dict__.keys())) # 3 session.flush() print(len(user.__dict__.keys())) # 4 session.commit() print(len(user.__dict__.keys())) # 1 user = User(name="test", password="test") print(len(user.__dict__.keys())) # 3 session.add(user) print(len(user.__dict__.keys())) # 3 session.flush() print(len(user.__dict__.keys())) # 4 session.commit() id = user.id print(len(user.__dict__.keys())) # 4 ``` The output seems that after session committed, the attribute of the object is not visible. But when you try to access any attribute, the behavior is excepted. |