Skip to content
Advertisement

How to add a cursor() of a db connection to a sqlalchemy.orm session? “sqlalchemy.orm.exc.UnmappedInstanceError: Class ‘builtins.int’ is not mapped”

I am in a Ubuntu 22.04 Docker container with Python 3.10.

I use these apt packages:

JavaScript

I use the following Python packages:

JavaScript

The sessionmaker parameter autoflush is set to True.

I want to add a deletion and after that an insertion to a sqlalchemy.orm session so that I commit only when the two commands worked out well. The aim of this is to update a table that gets updated over the day. I do not want to delete anything before I am not sure that the insertion really works.

The part of the Python code that leads to this error (without the follow-up insertion command):

JavaScript

I do not get the clue from other links on the same error:

The User model contains many Task models and the Task model contains many Subtask models.

…insert a new User into a DB using SQLAlchemy and Marshmallow.

I am new to sqlalchemy.orm and I fear that I have misunderstood something. I want to use a session for deletion and afterwards insertion of records, and I only want to commit in the end of the two commands. I use a cursor for the deletion. How can I embed the cursor – which is only available through the conn object from conn = engine.raw_connection() – so that the task is done only if the next insertion task works as well? I cannot just add it to the session:

JavaScript

Which throws:

JavaScript

The error in detail:

JavaScript

So how should I do it?

Advertisement

Answer

JavaScript

It turns out using the orm delete above returns a cursor, you can read about it here: update-and-delete-with-arbitrary-where-clause

The core version, using a table object, should work with session.execute and you can read about it here: getting-affected-row-count-from-update-delete

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement