django-pyodbc-azure icon indicating copy to clipboard operation
django-pyodbc-azure copied to clipboard

The data types nvarchar and ntext are incompatible in the equal to operator

Open aldon-cmd opened this issue 7 years ago • 4 comments

I cant seem to run an initial migration. do you have any idea what the problem could be?

  Apply all migrations: admin, auth, contenttypes, sessions, thumbnail, user, viewflow
Running migrations:
  Applying contenttypes.0002_remove_content_type_name...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\Users\user\Envs\venv\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 155, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "C:\Users\user\Envs\venv\lib\site-packages\sql_server\pyodbc\schema.py", line 655, in remove_field
    constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
  File "C:\Users\user\Envs\venv\lib\site-packages\sql_server\pyodbc\introspection.py", line 278, in get_constraints
    """, [table_name])
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\user\Envs\venv\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\user\Envs\venv\lib\site-packages\sql_server\pyodbc\base.py", line 545, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The data types nvarchar and ntext are incompatible in the equal to operator. (402) (SQLExecDirectW); [42000] [Micros
oft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)')

aldon-cmd avatar Oct 19 '17 11:10 aldon-cmd

To compare nvarchar and ntext values, the ntext value needs to be explicitly cast to nvarchar.

PS: the NTEXT type has been depricate since 2005 (or maybe before) so it might be a good idea to upgrade those columns if possible.

nschonni avatar Oct 19 '17 16:10 nschonni

I get the same error when I try to migrate right after creating a new project. I only have django 1.11.0 and django-pyodbc-azure 1.11.0.0 installed along with their dependencies in the virtual environment. I saw ntext come up in google but Im not seeing how the code is making reference to ntext in this project or the previous one.

(vdjango_project) C:\Users\user\Desktop\django_project>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name...Traceback (most recent call last):
File "manage.py", line 22, in <module>
  execute_from_command_line(sys.argv)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
  utility.execute()
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\core\management\__init__.py", line 355, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
  self.execute(*args, **cmd_options)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\core\management\base.py", line 330, in execute
  output = self.handle(*args, **options)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
  fake_initial=fake_initial,
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
  state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
  state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
  state = migration.apply(state, schema_editor)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
  operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\migrations\operations\fields.py", line 154, in database_forwards
  schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\sql_server\pyodbc\schema.py", line 655, in remove_field
  constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\sql_server\pyodbc\introspection.py", line 278, in get_constraints
  """, [table_name])
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\backends\utils.py", line 80, in execute
  return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\backends\utils.py", line 65, in execute
  return self.cursor.execute(sql, params)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\utils.py", line 94, in __exit__
  six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\django\db\backends\utils.py", line 65, in execute
  return self.cursor.execute(sql, params)
File "C:\Users\user\Envs\vdjango_project\lib\site-packages\sql_server\pyodbc\base.py", line 545, in execute
  return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]The data types nvarchar and ntext are incompatible in the equal to operator. (402) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)')

aldon-cmd avatar Oct 19 '17 17:10 aldon-cmd

I got exactly the same problem and fix it.

To avoid this situation, you should add driver name in django database settings. Such as

'default': {
    'NAME': 'xxx',
    'ENGINE': 'sql_server.pyodbc',
    'HOST': r'127.0.0.1',
    'USER': 'sa',
    'PASSWORD': 'xxx',
    'OPTIONS': {
        'driver': 'SQL Server Native Client 10.0'
    },
},

StarForceP avatar Nov 10 '17 07:11 StarForceP

I guess you're using an old SQL Server (WDAC) driver which treats nvarchar as ntext and that's why you're experiencing the issue because there's no reference to ntext type inside django-pyodbc-azure. See the following document for more details about the difference between an old WDAC driver and modern drivers for SQL Server. https://docs.microsoft.com/en-us/sql/relational-databases/native-client/applications/updating-an-application-to-sql-server-native-client-from-mdac

I strongly recommend that you install Microsoft ODBC Driver for SQL Server on your computer and utilize it from your Django app by specifying its name as the driver option in DATABASES settings. https://docs.microsoft.com/en-us/sql/connect/odbc/windows/microsoft-odbc-driver-for-sql-server-on-windows

michiya avatar Feb 27 '18 16:02 michiya