Django, ManyToManyField - ProgrammingError: relation foo_bar does not exist. Recognized in migrations, though relation is never created

Django, ManyToManyField - ProgrammingError: relation foo_bar does not exist. Recognized in migrations, though relation is never created

在这种情况下,"foo_bar" 实际上是 "links_userprofile_favorite_feeds"。

问题是,当我进入 manage.py shell 时:

manage.py外壳

from django.contrib.auth.models import User
from feeds.models import feed
p = User.objects.get(username='myuser')
p.save()
q = Feed.objects.get(title='myfeed')
q.save()
p.userprofile.favorite_feed.add(q)

我明白了

错误:

ProgrammingError: relation "links_userprofile_favorite_feeds" does not exist
LINE 1: ..."links_userprofile_favorite_feeds"."feed_id" FROM "links_use...

相关文件和回溯如下:

links.models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User,unique=True)
    bio = models.TextField(null=True)
    thumbnail = models.ImageField(upload_to="uploaded_files/")  
    favorite_feeds = models.ManyToManyField(Feed)

feeds.models.py

class Feed(models.Model):
    title = models.CharField(max_length=25)
    slug = models.SlugField(max_length=25)

    def save(self, *args, **kwargs):
        if not self.slug:
            #Newly created object, so set slug
            self.slug = slugify(self.title)

        super(Feed,self).save(*args,**kwargs)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ('title',)

该关系似乎存在于迁移中,但是 manage.py syncdb, manage.py makemigrations, manage.py migrate,所有都不起作用(没有要应用的迁移)。

有人能帮忙吗?我想创建关系 "links_userprofile_favorite_feeds."

已修复。

对于以后遇到这个问题的人:

  1. 从数据库中删除所有 links_* 表(应用被调用 'links')
  2. 通过执行以下操作删除 'links' 应用程序的所有迁移:

    from django.db.migrations.recorder import MigrationRecorder
    MigrationRecorder.Migration.objects.filter(app='links').delete()
    
  3. 向前迁移 manage.py migrate