6

While trying to dumpdata in Django with the following command:

python manage.py dumpdata --all --output /data/django_dump.json --verbosity 3

I get the following error:

CommandError: Unable to serialize database: ('HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server]Connection is busy with results for another command (0) (SQLExecDirectW)')
Exception ignored in: <generator object _cursor_iter at 0x7f1c4112b0c0>
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/sql_server/pyodbc/compiler.py", line 133, in _cursor_iter
    cursor.close()
  File "/opt/conda/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 500, in close
    self.cursor.close()
pyodbc.ProgrammingError: The cursor's connection has been closed.

Following the advice of this post, here's my database configuration:

DATABASES = {
    'default': {
        'NAME': ...,
        'ENGINE': 'sql_server.pyodbc',
        'HOST': ...,
        'USER': ...,
        'PASSWORD': ...,
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'MARS_Connection': True,
        }
    }
}

Using the MARS connection doesn't seem to help at all. Is my configuration wrong? Is there some other reason this might not work?

Python>=3.6, Django>=2, using MS SQL Server (not sure which version, nothing too old) and django-pyodbc-azure from conda-forge.

4
  • did you find any solution? Commented Mar 22, 2019 at 6:20
  • @prof.phython yeah, to not use SQL Server. I wish I could give better news, but at some point I just gave up trying to use MSSS with Python and switched to Postgres, and I don't think I figured this one out before making that switch. Commented Mar 22, 2019 at 17:25
  • 6
    Coming in much later to this, but 'MARS_Connection': True isn't technically correct (should be 'Yes'), but this also never worked for me. I had to specify 'extra_params': 'MARS_Connection=Yes'. Hope this helps someone Commented Feb 22, 2020 at 15:59
  • @WillGordon you helped me a lot. Thanks! :) Commented Apr 23, 2020 at 15:23

1 Answer 1

9

I tried around setting the MARS_Connection parameter and this is what finally worked (Django 3.0, django-mssql-backend 2.8.1) i.e. putting 'extra_params': 'MARS_Connection=Yes' inside of the OPTIONS:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT'),
        'TEST': {
            'NAME': os.getenv('DB_NAME'),
        },
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'extra_params': 'MARS_Connection=Yes'
        },
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this solved a problem during a pretty long migration!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.