【问题标题】:Unable to migrate models from django to sql server无法将模型从 django 迁移到 sql server
【发布时间】:2019-09-15 04:18:28
【问题描述】:

我正在尝试学习 django,必须使用 sql server 作为数据库,无法使用 manage.py migrate 迁移模型

我正在使用 django 2.1.8 数据库 蟒蛇3.7.3 我尝试重新安装 django、python 甚至 sql server,但都没有解决问题。

在运行 py manage.py migrate 时,我收到以下错误

    py manage.py migrate
    Operations to perform:

    Apply all migrations: admin, auth, contenttypes, sessions, testApp
    Running migrations:

    Applying contenttypes.0001_initial...Traceback (most recent call last):

    File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)

    File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)

    pyodbc.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')

    The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)

 File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\models.py", line 514, in database_forwards
getattr(new_model._meta, self.option_name, set()),

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 360, in alter_unique_together
self.execute(self._create_unique_sql(model, columns))

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\schema.py", line 653, in execute
cursor.execute(sql, params)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)

File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')

我希望它能够迁移,但它给了我这个错误。

【问题讨论】:

标签: python sql sql-server django migrate


【解决方案1】:

可能是一个不同的问题,但我遇到了同样的错误。在我的情况下,我已经运行python manage.py migrate --fake(每个其他 SO 线程)来修复我之前遇到的错误,并且 django 在没有实际创建表的情况下记录了迁移。当我再次运行它时,它假定我正在修改一个不存在的表。

我最终通过以下方式解决了这个问题:

  1. 在 SQL Server 中手动创建包含所有正确字段的表
  2. 注释掉models.py中各处的型号代码
  3. python manage.py makemigrations 删除注册表中的假表
  4. python manage.py migrate 删除我刚刚创建的手动表并更新迁移注册表
  5. 然后将表格重新添加到我的models.py
  6. 然后重新运行python manage.py makemigrations
  7. python manage.py migrate 将 django 与 SQL server 同步

【讨论】:

  • 如何取消--fake? python manage.py migrate --fake myapp 00XX_last_migration
猜你喜欢
  • 2015-01-05
  • 2021-07-25
  • 2020-09-29
  • 2018-04-07
  • 2019-07-12
  • 2012-09-04
  • 2011-07-02
  • 2010-12-18
  • 1970-01-01
相关资源
最近更新 更多