django.db.utils.OperationalError:my_table没有列ID错误?

时间:2014-03-15 17:39:43

标签: python sql django django-models

我正在测试我的Django应用程序的models.py文件:" myapp"在我的Python interepreter中,但一直遇到这个错误:

django.db.utils.OperationalError: table myapp_table1 has no column named attribute1_id

我的models.py,table1和表2中有2个表。

 class table1 (models.Model):
   name = models.CharField(max_length=25)
   def __str__(self):             
     return self.name
 class table2 (models.Model):
   name = models.CharField(max_length =25)
   attribute1= models.ForeignKey(Type, related_name = "att1_set+", default=None)
   attribute2= models.ManyToManyField(Type, related_name = "att2_set+", default=None)
   def __str__(self):             
     return self.metric_name

在我的python解释器中:

>>> from my_app.models import table1, table2
>>> t1 = table1 (name = "ty1", id = 0)
>>> t1.save()
>>> t2 = table1 (name = "ty2", id = 1)
>>> e1 = table2 (name = "as", attribute1 = t1)
>>> e1.save() 
>>> error aforementioned after .save

但是当我执行:" manage.py sql myapp"时,我看到了我的table2:

CREATE TABLE "my_app_table2" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(25) NOT NULL,
"attribute1_id" integer NOT NULL REFERENCES "myapp_table1" ("id")
)

2 个答案:

答案 0 :(得分:3)

您必须运行python manage.py syncdb(如果您使用python manage.py migrate,则需要south - 以便将更改应用于数据库。

答案 1 :(得分:0)

首先删除pycache,然后删除所有0001_initial.py,然后删除python manage.py flush,然后

python manage.py makemigrations

然后

python manage.py migrate

然后

python manage.py createsuperuser