Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

In this case, "foo_bar" is actually "links_userprofile_favorite_feeds".

The problem is, when I go into manage.py shell:

manage.py shell

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)

I get

the error:

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

Here are the relevant files and traceback:

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',)

The relation seems to be present in the migration, but manage.py syncdb, manage.py makemigrations, manage.py migrate, all do not work (no migrations to apply).

Can anyone help? I am wanting to create the relation "links_userprofile_favorite_feeds."

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Fixed.

For those with this problem in the future:

  1. Delete all links_* tables from the database (app was called 'links')
  2. Delete all migrations for the 'links' app by doing:

    from django.db.migrations.recorder import MigrationRecorder
    MigrationRecorder.Migration.objects.filter(app='links').delete()
    
  3. Migrated forward with manage.py migrate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...