Last active
August 29, 2016 04:53
-
-
Save chongkong/1460884de96866d42f5a197fcec5652b to your computer and use it in GitHub Desktop.
get() query does not load joinedloaded relationship when reloading expired object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import Column, BigInteger, ForeignKey, create_engine | |
from sqlalchemy.orm import relationship, sessionmaker | |
from sqlalchemy.ext.declarative import declarative_base | |
Base = declarative_base() | |
Session = sessionmaker() | |
class Foo(Base): | |
__tablename__ = 'foo' | |
id = Column(BigInteger, primary_key=True, autoincrement=True) | |
bars = relationship('Bar', backref='foo', lazy='joined') | |
class Bar(Base): | |
__tablename__ = 'bar' | |
foo_id = Column(BigInteger, ForeignKey('foo.id'), primary_key=True) | |
bar_id = Column(BigInteger, primary_key=True, autoincrement=False) | |
class Hello(Base): | |
__tablename__ = 'hello' | |
id = Column(BigInteger, primary_key=True, autoincrement=True) | |
def main(sqlalchemy_dabatase_uri): | |
engine = create_engine(sqlalchemy_dabatase_uri, echo=True) | |
Base.metadata.create_all(bind=engine) | |
Session.configure(bind=engine) | |
session = Session() | |
try: | |
foo = Foo() | |
session.add(foo) | |
session.commit() | |
hello = Hello() | |
session.add(hello) | |
session.commit() | |
print('----------') | |
session.query(Foo).get(1) | |
print('----------') | |
except: | |
pass | |
Base.metadata.drop_all(bind=engine) | |
if __name__ == '__main__': | |
main('mysql+pymysql://local:Password!2@localhost:3306/test_utf8mb4?charset=utf8mb4') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import Column, BigInteger, ForeignKey, create_engine | |
from sqlalchemy.orm import relationship, sessionmaker | |
from sqlalchemy.ext.declarative import declarative_base | |
Base = declarative_base() | |
Session = sessionmaker() | |
class Foo(Base): | |
__tablename__ = 'foo' | |
id = Column(BigInteger, primary_key=True, autoincrement=True) | |
bars = relationship('Bar', backref='foo', lazy='joined') | |
class Bar(Base): | |
__tablename__ = 'bar' | |
foo_id = Column(BigInteger, ForeignKey('foo.id'), primary_key=True) | |
bar_id = Column(BigInteger, primary_key=True, autoincrement=False) | |
def main(sqlalchemy_dabatase_uri): | |
engine = create_engine(sqlalchemy_dabatase_uri, echo=True) | |
Base.metadata.create_all(bind=engine) | |
Session.configure(bind=engine) | |
session = Session() | |
try: | |
foo = Foo() | |
foo.bars.append(Bar(bar_id=1)) | |
foo.bars.append(Bar(bar_id=2)) | |
session.add(foo) | |
session.commit() | |
print('----------') | |
foo.bars | |
print('----------') | |
except: | |
pass | |
Base.metadata.drop_all(bind=engine) | |
if __name__ == '__main__': | |
main('mysql+pymysql://local:Password!2@localhost:3306/test_utf8mb4?charset=utf8mb4') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample2.py run result: