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.

Getting sqlalchemy.exc.IntegrityError when executing simultaneous inserts.

See original GitHub issue

Hi,

Thank you so much for creating SQLAlchemy!

Summary

We are currently seeing the following error:

sqlalchemy.exc.IntegrityError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)
(mysql.connector.errors.IntegrityError) 1062 (23000): Duplicate entry '1-62' for key 'user_id_item_id'
[SQL: INSERT INTO item_user_associations (user_id, item_id, extra_data) VALUES (%(user_id)s, %(item_id)s, %(extra_data)s)]
[parameters: {'user_id': 1, 'item_id': 62, 'extra_data': 'hello world!'}]
(Background on this error at: http://sqlalche.me/e/gkpj)

We believe the error occurs when users send two POST requests to our API containing similar data in very close succession. However, the issue is reproducible without networking.

Here is an example PyCharm project to reproduce the issue: AssociationErrorExample-test.zip

Steps to Repro

  1. Get a local copy of MySQL running:
     docker run --name my_db \
          -e MYSQL_DATABASE=my_db \
          -e MYSQL_USER=user \
          -e MYSQL_PASSWORD=password \
          -p 3306:3306 -d mysql/mysql-server:5.7
    
  2. Run the “Compound” run configuration from PyCharm:
    Run > Run 'Compound'
    
    This executes the “example” run configuration 8 times in parallel.
  3. As an alternative you can execute example.py in multiple terminal windows.

Expected: No error.

Actual: You see an sqlalchemy.exc.IntegrityError error in at least one of the outputs.

Workaround

We’ve been doing something like this to overcome this issue:

@contextmanager
def session_scope():
    """
    This code is taken from the final example here:
    https://docs.sqlalchemy.org/en/13/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it
    """
    session = Session()
    try:
        yield session
        session.commit()
    except exc.InternalError:
        session.rollback()
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()

Questions

Is there a built in SQLAlchemy mechanism or recommended best practice to overcome this issue?

Please let me know. Thanks!

-Nick

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
advance512commented, Jun 27, 2020

@zzzeek Are there plans to support “upsert” operations for MySQL (and other databases that support it, e.g. sqlite) in the ORM, as opposed to Core?

I have spent hours and found no simple way to add support to a single ORM model to do “upserts” instead of normal inserts. Everything requires dropping down to Core, or performing global changes to all models.

0reactions
zzzeekcommented, Jul 5, 2020

added this at #5441

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot catch SQLAlchemy IntegrityError - Stack Overflow
I have the same need in my Flask application, I handle it like below and it works: from flask import Flask from flask_sqlalchemy...
Read more >
Why am I getting sqlalchemy.exc.integrityerror in routes using ...
I have a function that the user can add a budget. In the python shell, when I am adding a budget there is...
Read more >
Error Messages - SQLAlchemy 1.4 Documentation
IntegrityError ¶. Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails. This error is a...
Read more >
Bulk Insert Broken for Polymorphism? - Google Groups
If you run it on Postgresql you get: sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) null value in column "MyId" violates not-null constraint
Read more >
Duplicate key exception while inserting data (#55) · Issues
py", line 552, in do_execute cursor.execute(statement, parameters) sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) ERREUR: la ...
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