Integrity Error: Updating or deleting violates a foreign key constraint. Django + PostgreSQL

This modification is UserProfile

 class UserProfile(models.Model):
        user = models.OneToOneField(User)
        fb_id = models.IntegerField(primary_key=True,null=False,blank=True)
        follows = models.ManyToManyField('self', related_name='followed_by', symmetrical=False)
    User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

I get the following error after trying to delete all test users or any of them.

    django.db.utils.IntegrityError: update or delete on table "blog_userprofile" violates foreign key constraint "blog_from_userprofile_id_a482ff43f3cdedf_fk_blog_userprofile_id" on table "blog_userprofile_follows"
DETAIL:  Key (id)=(4) is still referenced from table "blog_userprofile_follows".

The default cascading delete is true, why am I getting this and how can I fix it? I am using PostgreSQL + Django 1.8

EDIT: slight condition: I'v changed primary_keythe default to fb_id. And there were duplicates because it was not unique. Therefore, this error occurs when I try to migrate / makemigrations / syncdb:

   django.db.utils.IntegrityError: could not create unique index "blog_userprofile_fb_id_ce4e6e3086081d4_uniq"
DETAIL:  Key (fb_id)=(0) is duplicated.

This is why I tried to delete all test users

And when I tried to reset everything, I tried to return to the default primary key, I get:

django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "blog_userprofile"
+7
2

, " ", - PostgreSQL MySQL. .

enter image description here

. : PostgreSQL

, .

+2

Mysql Postgres, . , "makemigrations" "migrate", postgres, !

-1

Source: https://habr.com/ru/post/1666935/


All Articles