Running populate_history command fails with mssql-django #1417
Description
Describe the bug
When we run the command python manage.py populate_history --auto --batchsize 500
, we get an error:
django.db.utils.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]New transaction is not allowed because there are other threads running in the session. (3988) (SQLSetConnectAttr)')
Related:
To Reproduce
Steps to reproduce the behavior:
- Use MS SQL.
- We are using the docker image
mcr.microsoft.com/mssql/server:2017-latest
- In django's
DATABASES
settings, underOPTIONS
, we passTrustServerCertificate=yes;MARS Connection=True
forextra_params
[^1]
- We are using the docker image
- Have a model with instances that exceeds the
batch_size
you will pass in step 5 [*] - Follow setup instructions of django-simple-history (install package, setup middleware, etc.) up to running the
populate_history
command - Run the
populate_history
command.
[*] The point is to reproduce the scenario where the iterator (server-side db cursor) is not done returning rows to Django. If you set --batch_size
to 1 and you have a model that has more than 1 instance, you should also experience the same error.
[^1] Our django databases settings would look something like,
"default": {
"ENGINE": "mssql",
"NAME": DB_NAME,
"USER": DB_USER,
"PASSWORD": DB_PWD,
"HOST": DB_SERVER,
"PORT": DB_PORT,
"OPTIONS": {
"driver": "ODBC Driver 18 for SQL Server",
"return_rows_bulk_insert": False,
"extra_params": "TrustServerCertificate=yes;MARS_Connection=yes",
},
Actual behavior
We get an error message:
django.db.utils.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]New transaction is not allowed because there are other threads running in the session. (3988) (SQLSetConnectAttr)')
In every run of populate_history
, the logs just before the error is raised shows:
- The last model the command worked on is different in every run
- The number of models the command worked on is different in every run
Expected behavior
Running populate_history
is successful.
Screenshots
This is the stack trace,
Environment (please complete the following information):
- OS: Ubuntu 24.04
- Django Simple History Version: 3.7.0
- Django Version: 4.2.16
- Database Version: mcr.microsoft.com/mssql/server:2017-latest
- mssql-django==1.5.0