question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

register_entities | InvalidRequestError: A transaction is already begun for this connection

See original GitHub issue

Hello,

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:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
quoimeccommented, Oct 19, 2021

Seems to be working for me too, thanks for the quick fix on this @olirice!

1reaction
ddemidovcommented, Oct 18, 2021

I can confirm the error with undefined sess is gone. There is another problem with async engine, I’ll open another issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLAlchemy session.begin() giving transaction error when ...
InvalidRequestError: A transaction is already begun. Use subtransactions=True to allow subtransactions" error? Following the best practices of ...
Read more >
Explicit commit in transaction context manager causes error.
InvalidRequestError ( sqlalchemy.exc.InvalidRequestError: A transaction is already begun. Use subtransactions=True to allow subtransactions.
Read more >
Why do I have to begin a transaction? - Google Groups
That raises "sqlalchemy.exc.InvalidRequestError: No transaction is begun." To work around that I have to put "sess.begin()" after creating the session.
Read more >
Session API — SQLAlchemy 2.0 Documentation
Begin a transaction, or nested transaction, on this Session , if one is not already begun. The Session object features autobegin behavior, ...
Read more >
Session - API Manual
Session = sessionmaker() # bind an individual session to a connection sess ... that this begin() can create a subtransaction if a transaction...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found