Django:用户错误的外键

时间:2016-05-04 09:09:21

标签: mysql django

我在我的数据库中的每个表中都有这些行。我使用的是django 1.8。

pub_date = models.DateTimeField(default=datetime.datetime.now)
author = models.ForeignKey(settings.AUTH_USER_MODEL)

我也尝试了下面的代码并得到了同样的错误。

author = models.ForeignKey(User)

这是我在终端中使用syncdb看到的内容。

cy_thal是我的数据库的名称。

C:\Python27\Lib\site-packages\django\core\management\commands\syncdb.py:24: Remo
vedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDja
ngo19Warning)

Operations to perform:
  Synchronize unmigrated apps: chartit, pagination, staticfiles, autocomplete_li
ght, messages, bootstrap3_datetime, eReg, crispy_forms, bootstrap3
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table eReg_demographic
    Creating table eReg_icd_10
    Creating table eReg_pregnancy
    Creating table eReg_diagnosis
    Creating table eReg_clinical_data
    Creating table eReg_clinical_data_two
    Creating table eReg_a_b_sickle_thal
    Creating table eReg_redcell_enzyme_dis
    Creating table eReg_redcell_membrane_dis
    Creating table eReg_cong_dyseryth_anaemia
    Creating table eReg_ext_centers
    Running deferred SQL...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
    utility.execute()
  File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line
330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 390,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\Lib\site-packages\django\core\management\commands\syncdb.py"
, line 25, in handle
    call_command("migrate", **options)
  File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line
120, in call_command
    return command.execute(*args, **defaults)
  File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\Lib\site-packages\django\core\management\commands\migrate.py
", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)

  File "C:\Python27\Lib\site-packages\django\core\management\commands\migrate.py
", line 317, in sync_apps
    cursor.execute(statement)
  File "C:\Python27\Lib\site-packages\django\db\backends\utils.py", line 79, in
execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\Lib\site-packages\django\db\backends\utils.py", line 64, in
execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\Lib\site-packages\django\db\utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\Lib\site-packages\django\db\backends\utils.py", line 62, in
execute
    return self.cursor.execute(sql)
  File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 12
4, in execute
    return self.cursor.execute(query, args)
  File "C:\Python27\Lib\site-packages\MySQLdb\cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\Lib\site-packages\MySQLdb\connections.py", line 36, in defau
lterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'cy_thal.#sql-f8c_9
' (errno: 150)")

你知道如何解决这个问题吗?

提前谢谢!

2 个答案:

答案 0 :(得分:0)

不完全确定哪个是错误,但我会尝试下一个。

更改模型后执行以下行:
    python manage.py makemigrations appname
    python manage.py migrate appname

如果错误仍然存​​在,请尝试:     python manage.py syncdb

编辑:我编辑了丹尼尔罗斯曼建议的答案。 Django 1.8支持迁移。无需使用南方。

答案 1 :(得分:0)

在某些情况下,必须在其他应用之前迁移auth应用,因为它会生成所有权限,以及常用的模型(如User)的索引,这些是设置外部所必需的其他模型中的关键约束。

运行./manage.py migrate auth,然后您就可以执行标准./manage.py migrate