“编辑错误:关系”music_album“的列”genre_id“不存在”,而列确实存在

时间:2013-11-23 22:16:47

标签: python django postgresql orm

我有以下型号:

class CulturalDocument(CacheMixin, models.Model):
    ...
    uuid = UUIDField(unique=True)


class Genre(CulturalDocument):
    name = models.CharField(max_length=32)
    ...

class Album(CulturalDocument):
    ...
    genre = models.ForeignKey(Genre, null=True, blank=True)

我添加了genre属性以及南迁移。

我可以使用pg_admin在表genre_id中看到列music_album

然而,当我这样做时:

    album = Album.objects.create(uuid=3,
                                 release_date=datetime(2000, 1, 1),
                                 title="Fantastic Album",
                                 right_holder=rh)

我明白了:

 "ProgrammingError: column "genre_id" of relation "music_album" does not exist" while the column does exist
 LINE 1: ..._id", "title", "release_date", "right_holder_id", "genre_id"...
 .                                                            ^

在Ubuntu 12.04中使用PostGres 9.2和Django 1.6。

生成的SQL是:

INSERT INTO "music_album" ("culturaldocument_ptr_id", "title", "release_date", "right_holder_id", "genre_id") VALUES (%s, %s, %s, %s, %s)

2 个答案:

答案 0 :(得分:5)

发现它!

运行单元测试时出现此错误。迁移时,我在常规数据库上应用了迁移,但单元测试使用不同的数据库。

Py.test,我的测试工具,设置为刷新表,但不是在测试之间删除它们,以加快速度。因此,测试表使用迁移之前存在的方案。我刚删除它们并强制py.test重新创建它们。

答案 1 :(得分:0)

从我能看到的代码开始:

  album = Album.objects.create(uuid=3,
                             release_date=datetime(2000, 1, 1),
                             title="Fantastic Album",
                             right_holder=rh)

传递4个元素而表需要5 (你缺少genre_id)。尝试:

  album = Album.objects.create(uuid=3,
                             release_date=datetime(2000, 1, 1),
                             title="Fantastic Album",
                             right_holder=rh,
                             genre_id=VALID_ID)

其中VALID_ID是该类型的有效值。