Skip to content

Could not find stored procedure 'sp_msforeachtable' #26

Closed
@BobDenar1212

Description

@BobDenar1212

Hi,

I'm deploying a django application into a Azure App Service for linux, the database is an Azure SQL Database

Context

python 3.8
Django 3.0.13
mssql-django 1.0b1
pyodbc 4.0.30

Description

While loading data from fixtures with the python manage.py loaddata ... command, it fails with the following trace:

 Traceback (most recent call last):
   File "manage.py", line 22, in <module>
     main()
   File "manage.py", line 18, in main
     execute_from_command_line(sys.argv)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
     utility.execute()
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
     output = self.handle(*args, **options)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
     self.loaddata(fixture_labels)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 111, in loaddata
     with connection.constraint_checks_disabled():
   File "/opt/python/3.8.6/lib/python3.8/contextlib.py", line 113, in __enter__
     return next(self.gen)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 459, in constraint_checks_disabled
     disabled = self.disable_constraint_checking()
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/mssql/base.py", line 490, in disable_constraint_checking
     self.cursor().execute('EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL"')
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
     return self.cursor.execute(sql, params)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql)
   File "/home/site/wwwroot/antenv/lib/python3.8/site-packages/mssql/base.py", line 561, in execute
     return self.cursor.execute(sql, params)
 django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not find stored procedure 'sp_msforeachtable'. (2812) (SQLExecDirectW)")

This stored procedure seems to be not available in Azure SQL Database.

Adding the missing stored procedure sp_MSforeachtable and its dependency sp_MSforeach_worker does not help because those procedures are called from master database, and I don't have the credentials to add it to master.

I could not find a workaround, even with adding ATOMIC_REQUESTS = True to the Django DATABASES settings, since it is called in methods DatabaseWrapper.disable_constraint_checking and DatabaseWrapper.enable_constraint_checking if property needs_rollback is False in file mssql/base.py

I may have made something wrong since django-mssql documentation tells that it supports Azure SQL Database.

Any help would be much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions