inspectdb issue

Hi All,

Good Morning!!! I am new to Django development and I am having the following issue.

My scenario is as follows:

  1. I have Microsoft SQL Server with database “timesheet” already (with 10 tables)

  2. I have created an environment with Python 3.9.7 and django = 3.2 and also have django-mssql-backend = 2.8.1 and mssql-django = 1.1.2

  3. I have created the project and an application first_app

  4. I wanted to get the models in SQL server in [models.py]
    #python manage.py inspectdb
    but this gives the following error

Unable to inspect table ‘Tbl_Employee’

The error was: () missing 1 required positional argument: ‘collation’

Unable to inspect table ‘Tbl_EmployeeGroup’

The error was: () missing 1 required positional argument: ‘collation’

and the same error comes for all other tables.

  1. I was first using django 4.0.2 and there too in the inspectDB i got the same error so i created this new environment with django to 3.2 - I am getting the same error in both environments

Could you please help with the following scenario.

Kindly let me know if you require any further info.

Thanks,
Boomessh V

It doesn’t make sense to have both - you can only use one. Check your DATABASES setting to which one you’re using - I suspect it’s mssql-django, since it’s newer (mssql-django is a fork of django-mssql-backend, now maintained by microsoft).


As to the error: Django provides the inspectdb command, but it’s up to the database backend to ensure that it works. Therefore this is a bug with django-mssql-backend or mssql-django. Report the bug there, they won’t see it on the forums. I’m sure they will ask for more details regarding the tables in question - perhaps you can show them the SQL CREATE TABLE statements.

To make things work for yourself, you can proceed by using inspectdb on the tables that it does work for (inspectdb table1 table2), then writing models by hand for the two failing tables. inspectdb is ultimately just a shortcut for generating some code - the end result is still your code that you’re responsible for, so you shouldn’t be afraid of writing your own.

Hope that helps and good luck!

Hi adamchainz,

Thanks for your reply and yes I will try to generate it (models) myself and proceed with the work.

Also, is this what you are mentioning to look at in the settings.py file

DATABASES = {
‘default’: {
‘ENGINE’: ‘sql_server.pyodbc’,
‘NAME’: ‘TIMESHEET’,
‘USERNAME’: ‘’,
‘PASSWORD’: '
’,
‘HOST’: ‘<>’,
‘DRIVER’: ‘{ODBC Driver 17 for SQL Server}’,
}

This is the working connection string.

Thanks,
Boomessh V

From ENGINE, it looks like you’re using django-mssql-backend: django-mssql-backend · PyPI

If you were using mssql-django, your ENGINE would be mssql: GitHub - microsoft/mssql-django: The Microsoft SQL Server 3rd Party Backend for Django provides a connectivity layer for Django on SQL Server or Azure SQL DB.

Since mssql-django is the newer package, try that instead. They may have improved inspectdb when they forked the package. Otherwise, report the bug on their GitHub.

Hi adamchainz,

Thanks a ton for your reply.
It finally worked after changing the engine to mssql and I am getting the models.py generated based on my sql database tables.

Is there any site describing the common reference for the ENGINE to the dll mappings (like mssql → mssql-django or pyodbc refers to django-mssql-backend) - kindly let me know.

once again thanks for your reply,
Boomessh V

Hi @Boomesshv not sure if this will help, but i used this site when i started a few months ago to get an idea of the different Engines Django could use. However not all of them. There are links to Repo’s at the bottom as well, which Adam already referenced as well.

inspectDB can be tricky especially if you have multiple databases configured and if your SCHEMA is not the default one but a custom schema. These might pop up in the future as well :-).

Good luck.

Hi RaynoKriel

Thanks - I missed to see that, I checked it now and yes I think I understand it now.

Thanks,
Boomessh V

1 Like