【问题标题】:Foreign key constraint error on Django appDjango应用程序上的外键约束错误
【发布时间】:2014-03-27 05:54:38
【问题描述】:

我正在尝试将第三类 noticeTime 限制为外键 email。我使用与第二类 location 相同的语法,但是当我在 noticeTime 上使用它时会引发错误:

Exception Value: no such column: setupNotifications_noticetime.email_id

代码如下:

from django.db import models

# Create your models here.
from django.db import models

class email(models.Model):
    email = models.CharField(max_length=200)
    def __unicode__(self):
        return self.email`

class location(models.Model):
    email = models.ForeignKey(email)
    zip_code = models.CharField(max_length=5)
    def __unicode__(self):
        return self.zip_code

class noticeTime(models.Model):
    email = models.ForeignKey(email)
    time = models.CharField(max_length=200)
    def __unicode__(self):
        return self.time

这里是 admin.py:

from django.contrib import admin

# Register your models here.
from setupNotifications.models import email
from setupNotifications.models import location
from setupNotifications.models import noticeTime


admin.site.register(email)
admin.site.register(location)
admin.site.register(noticeTime)

我正在使用 sqlite 数据库

【问题讨论】:

  • 您发布的代码不会给您提到的错误。贴出完整代码。
  • 它是一个 django 应用程序,所以有很多其他文件被引用,不知道我将如何发布所有这些
  • 我猜你会在你的views.py中找到它访问noticeTime-Model?你能发布那个文件吗?
  • 您在哪里访问setupNotifications...或更好...您从哪里得到错误/您在做什么?
  • 请更新/编辑您的问题并添加您的admin.py的内容

标签: python django django-models


【解决方案1】:

也许您的问题是您运行了 syncdb,假设它会更改表以匹配您的模型更改。不幸的是,它没有这样做。有一些单独的工具可用,例如 South,它们可以帮助进行数据库迁移。

【讨论】:

  • 谢谢,这是一个很好的答案!
猜你喜欢
  • 1970-01-01
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2018-02-19
  • 2019-12-24
相关资源
最近更新 更多