register_entities | InvalidRequestError: A transaction is already begun for this connection
See original GitHub issueHello,
I set up an extremely basic test view in my env.py file to try out this package but when I ran alembic revision --autogenerate
, alembic_utils raised the following exception:
File "/usr/local/lib/python3.7/site-packages/alembic_utils/replaceable_entity.py", line 251, in compare_registered_entities
sess.rollback()
UnboundLocalError: local variable 'sess' referenced before assignment
I dug into the replaceable_entity file and found that the culprit for this issue is this block of code:
try:
transaction = connection.begin()
sess = Session(bind=connection)
ordered_entities: List[T] = solve_resolution_order(sess, entities)
finally:
sess.rollback()
The core of the problem appears to be the connection.begin()
line, which raises the following silent exception, causing sess to be undeclared:
File "/usr/local/lib/python3.7/site-packages/alembic_utils/replaceable_entity.py", line 247, in compare_registered_entities
transaction = connection.begin()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/future/engine.py", line 172, in begin
return super(Connection, self).begin()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 741, in begin
"a transaction is already begun for this connection"
sqlalchemy.exc.InvalidRequestError: a transaction is already begun for this connection
I found that I was able to avoid this issue if I simply commented out the transaction definition as it also appears to not be used as part of the proceeding code. Any ideas? I am confused as to why this is not an issue that has been raised before. After disabling the line, I was able to run my revision script without issue and correctly generate my test view.
pip reference:
alembic 1.6.4
alembic-utils 0.7.2
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
Seems to be working for me too, thanks for the quick fix on this @olirice!
I can confirm the error with undefined
sess
is gone. There is another problem with async engine, I’ll open another issue.