Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running populate_history command fails with mssql-django #1417

Open
paxw-panevo opened this issue Oct 30, 2024 · 0 comments
Open

Running populate_history command fails with mssql-django #1417

paxw-panevo opened this issue Oct 30, 2024 · 0 comments

Comments

@paxw-panevo
Copy link

paxw-panevo commented Oct 30, 2024

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:

  1. Use MS SQL.
    • We are using the docker image mcr.microsoft.com/mssql/server:2017-latest
    • In django's DATABASES settings, under OPTIONS, we pass TrustServerCertificate=yes;MARS Connection=True for extra_params [^1]
  2. Have a model with instances that exceeds the batch_size you will pass in step 5 [*]
  3. Follow setup instructions of django-simple-history (install package, setup middleware, etc.) up to running the populate_history command
  4. 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,
image

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant