You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In SQL.execute, if autocommit is on (default behavior) and the SQL query fails due to an IntegrityError, the open transaction is never closed via COMMIT or ROLLBACK. Relevant snippet:
In this case, on line 331, a transaction is started, on 332 the statement fails, and thus line 334 is never executed. In the except clause, the transaction never gets completed either.
Then, when the next query is run, the BEGIN on line 331 causes RuntimeError: cannot start a transaction within a transaction because the previous transaction was never closed.
Being able to catch an IntegrityError is important for preventing race conditions (if the query fails, find out why as opposed to check if the record already exists, and then insert)
Minimal reproducible example (tested with cs50-9.3.0 on Ubuntu 20.04 (Python 3.8) and Windows 10 (Python 3.11):
fromcs50importSQLopen("test.db", "w").close()
db=SQL("sqlite:///test.db")
db.execute("CREATE TABLE test(id integer UNIQUE)")
db.execute("INSERT INTO test VALUES(1)")
try:
# this should fail because of the UNIQUE constraintdb.execute("INSERT INTO test VALUES(1)")
exceptValueErrorase:
print(e)
print(db.execute("SELECT * FROM test"))
Expected output:
UNIQUE constraint failed: test.id
[{'id': 1}]
Actual output:
UNIQUE constraint failed: test.id
Traceback (most recent call last):
File "\tmp\venv\test.py", line 13, in <module>
print(db.execute("SELECT * FROM test"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\tmp\venv\lib\site-packages\cs50\sql.py", line 29, in decorator
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "\tmp\venv\lib\site-packages\cs50\sql.py", line 413, in execute
raise e
RuntimeError: cannot start a transaction within a transaction
The text was updated successfully, but these errors were encountered:
Upon further testing, it seems like the upgrade to SQLAlchemy causes this bug: in 1.4.49, the transaction gets aborted when the IntegrityError is raised, and in 2.0.23, the transaction is not aborted. The sample code I provided initially does produce the right output with SQLAlchemy 1.4.49, but not with 2.0.23.
In
SQL.execute
, if autocommit is on (default behavior) and the SQL query fails due to an IntegrityError, the open transaction is never closed via COMMIT or ROLLBACK. Relevant snippet:In this case, on line 331, a transaction is started, on 332 the statement fails, and thus line 334 is never executed. In the except clause, the transaction never gets completed either.
Then, when the next query is run, the BEGIN on line 331 causes
RuntimeError: cannot start a transaction within a transaction
because the previous transaction was never closed.Being able to catch an IntegrityError is important for preventing race conditions (if the query fails, find out why as opposed to check if the record already exists, and then insert)
Minimal reproducible example (tested with cs50-9.3.0 on Ubuntu 20.04 (Python 3.8) and Windows 10 (Python 3.11):
Expected output:
Actual output:
The text was updated successfully, but these errors were encountered: