I have very simple code causing my MySQL db to freeze:
import sqlalchemy as sa
from sqlalchemy import orm
import utils
import config
utils.base_init(config)
Base = config.Base
class Parent(Base):
__tablename__ = 'Parents'
id = sa.Column(sa.Integer, primary_key=True)
children = orm.relationship('Child', backref='parent')
class Child(Base):
id = sa.Column(sa.Integer, primary_key=True)
parent_id = sa.Column(sa.Integer)
__tablename__ = 'Children'
__table_args__ = (sa.ForeignKeyConstraint(
['parent_id'],
['Parents.id'],
onupdate='CASCADE', ondelete='CASCADE'),{})
Base.metadata.create_all()
session = orm.sessionmaker(bind=config.Base.metadata.bind)()
p = Parent(id=1)
c1 = Child(id=1)
c2 = Child(id=2)
session.add(p)
session.add(c1)
session.add(c2)
session.commit()
c1.parent
Base.metadata.drop_all()
It seems that SQL Alchemy does not release the metadata lock after issuing the selection request needed to load the relation attribute? How can I free him? I don’t even understand why the select statement will have to lock the table first!
Of course, I can make this particular piece of code work by closing the session, but this is not practical in my real program.
source
share