Skip to content
Advertisement

SQLAlchemy bulk insert statement in Postgres database throws AttributeError

I am trying to insert rows in Python SQLAlchemy by bulk into a Postgres database by using an insert statement. I need to use the insert statement instead of bulk_insert_mappings, as I want to silently ignore failed insertion of duplicate entries. This was not apparent before, but I have added it now.

The table is created as it should. However, even a very simple insert operation via statement API throws this error:

JavaScript

Minimal Verifiable Example:

JavaScript

Full stacktrace:

JavaScript

Am I misusing the statement interface? The ORM statement looks fine:

JavaScript

I am using

  • PostgreSQL 14.4
  • psycopg2-binary 2.9.3
  • SQLAlchemy 1.4.39

Advertisement

Answer

I found a solution that uses insert statement: Avoid using the ORM statements. For some reason, using plain statements seems to do the job, whilst ORM ones throw the AttributeError.

This is confusing, as the official documentation calls for ORM statements:

JavaScript

But if you omit the ORM statement part, all is good

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