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.

SQLAlchemy 1.4.x: RootTransaction object cannot serve as stash storage for original method references

See original GitHub issue

The 1.4 major release of sqlalchemy seems to introduce some interface changes that pytest-flask-sqlalchemy 1.0.2 has trouble handling.

In particular one case is that pytest-flask-sqlalchemy will attempt to store a transaction’s original rollback method in a temporary / ephemeral force_rollback property, but the current sqlalchemy 1.4.0 release seems incompatible with this – I think because the RootTransaction class defines an explicit list of slots available on the object.

This issue might be a one-off, or it could be part of a larger category of sqlalchemy 1.4 compatibility concerns.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
itajajacommented, Apr 29, 2021

@jace we quite successfully dropped this package and we wrote our own fixture. this is the code, you might need to adapt it slightly for your needs

@pytest.fixture(autouse=True)
def enable_transactional_tests(db, connection):
    transaction = connection.begin()
    db.session = db.create_scoped_session(options={"bind": connection, "binds": {}})
    db.session.begin_nested()

    # for handling tests that actually call "session.rollback()"
    # https://docs.sqlalchemy.org/en/13/orm/session_transaction.html#joining-a-session-into-an-external-transaction-such-as-for-test-suites
    @event.listens_for(db.session, "after_transaction_end")
    def restart_savepoint(session, transaction_in):
        if transaction_in.nested and not transaction_in._parent.nested:
            session.expire_all()
            session.begin_nested()

    yield

    db.session.close()
    transaction.rollback()

where the db fixture is the db and the connection fixture is

@pytest.fixture(scope="session")
def connection(db):
    return db.engine.connect()
3reactions
jtfidjecommented, Apr 15, 2021

Here’s one I just had to deal with.

Whenever I use the db_session fixture this error is thrown:

AttributeError: 'RootTransaction' object has no attribute 'force_rollback'

... /site-packages/pytest_flask_sqlalchemy/fixtures.py:43: AttributeError

I’ve had to roll-back to SQLAlchemy v.1.3.24

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages - SQLAlchemy 1.4 Documentation
Object cannot be converted to 'persistent' state, as this identity map is no longer valid. AsyncIO Exceptions. AwaitRequired; MissingGreenlet; No Inspection ...
Read more >
Using the Snowflake SQLAlchemy Toolkit with the Python ...
Snowflake SQLAlchemy runs on the top of the Snowflake Connector for Python as a dialect to bridge a Snowflake database and SQLAlchemy applications....
Read more >
How to Use Flask-SQLAlchemy to Interact with Databases in a ...
You'll use it to create a database object that connects to your Flask ... and stored in the basedir variable, and the database.db...
Read more >
Declaring Models — Flask-SQLAlchemy Documentation (2.x)
Declaring Models¶ · The baseclass for all your models is called db.Model . It's stored on the SQLAlchemy instance you have to create....
Read more >
SQLAlchemy: get Model from table name. This may imply ...
Inspired by Eevee's comment: def get_class_by_tablename(tablename): """Return class reference mapped to table. :param tablename: String with name of table.
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