【问题标题】:DatabaseError "no such column"数据库错误“没有这样的列”
【发布时间】:2012-02-02 06:26:34
【问题描述】:

出于某种原因,我收到了DatabaseError "no such column: myapp_customer.redirect_link_id"。在另一台计算机上与我具有相同代码的人没有这个问题。我们使用 Django-South 进行迁移,据此我完全了解模型:

$ python manage.py schemamigration myapp --auto
Nothing seems to have changed.

$ python manage.py migrate myapp
Running migrations for myapp:
- Nothing to migrate.
 - Loading initial data for myapp.
No fixtures found.

这是完整的堆栈跟踪:

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/me/sources/django_myapp/../django_myapp/myapp/views.py" in main_page
  66.         customer = Customer.objects.select_related().get(user = request.user)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in get
  344.         num = len(clone)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in __len__
  82.                 self._result_cache = list(self.iterator())
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in iterator
  273.         for row in compiler.results_iter():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  680.         for rows in self.execute_sql(MULTI):
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  735.         cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/util.py" in execute
  34.             return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  234.             return Database.Cursor.execute(self, query, params)

Exception Type: DatabaseError at /
Exception Value: no such column: myapp_customer.redirect_link_id

我跑了manage.py sqlall myapp,根据它,redirect_link_id 存在于数据库中:

CREATE TABLE "myapp_customer" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id"),
    "client_key" varchar(9) NOT NULL UNIQUE,
    "api_key" varchar(30) NOT NULL UNIQUE,
    "redirect_link_id" integer REFERENCES "myapp_full_link" ("id"),
    "message_title" varchar(200),
    "message_body" text,
    "customer_group_id" integer NOT NULL REFERENCES "myapp_customer_group" ("id")
)
;

【问题讨论】:

  • “myapp_full_link”表是否存在且具有完整性?
  • sqlall 不会向您显示数据库的当前状态。它显示了从头开始设置数据库所需的 SQL。改用 dbshel​​l 命令,然后使用 '.schema'(用于 SQLite)
  • 所以根据sqllite,myapp_customer 表实际上没有redirect_link_id 列。 myapp_full_link确实存在问题:为什么南迁没有根据我的模型创建列?

标签: django django-models django-south


【解决方案1】:

您确定您还有来自 myapp/migrations 目录的最新迁移文件吗?

【讨论】:

  • 我很确定。我仔细检查了工作机器,那里没有未跟踪的迁移。
  • ./manage.py migrate --list 也可能会提供有关迁移状态的一些指示。除此之外,我建议在数据库级别检查实际的当前架构,并将其当前状态与 south_migrationhistory 表中的迁移记录进行比较,看看哪里出了问题。
  • 工作机器和非工作机器从该命令具有相同的输出。另外,见上文,我运行了 manage.py sqlall ,这似乎暗示我在实际数据库中有该列
  • 我开始没有建议了。您之前是否在您的机器上运行过migrate 命令,或者这是您第一次应用迁移?我不明白为什么不会应用迁移,除了之前使用 --fake 标志或其他东西在您的机器上应用迁移。
猜你喜欢
  • 2016-02-15
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2014-01-06
  • 2012-03-08
  • 1970-01-01
相关资源
最近更新 更多