首页 > 解决方案 > Django - Why is this Model not creating a pk and complaining about the integrity of the pk of a related model?

问题描述

Why is this Model not creating a pk and complaining about the integrity of the pk of a related model?

When creating a new instance of UserProfile it is not creating a primary key.

I'm following the one-to-one instructions in this tutorial (that's what all the @receiver stuff is about)

I have the following in my models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    guid = models.UUIDField(null=True)

    @classmethod
    def create(cls, first_name, last_name, email, guid=None):
        user = User.objects.create(first_name=first_name, last_name=last_name, email=email)
        user_profile = cls(user=user, guid=guid) if guid else cls(user=user)
        # user_profile.save()  ## This didn't work..
        return user_profile

    def most_recent_device(self):
        return self.devices.order_by('-pk').first()

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()


class Device(models.Model):
    guid = models.UUIDField(null=True)
    fcm_token = models.CharField(max_length=4096, null=True)
    user_profiles = models.ManyToManyField(UserProfile, related_name='devices')

    def most_recent_user(self):
        return self.user_profiles.order_by('-pk').first()

    @classmethod
    def create(cls, guid=None, fcm_token=None, user_profiles=None):
        self = cls()
        self.save()  ## possibly excessive saving trying to get this to work
        if guid:
            self.guid = guid
        if fcm_token:
            self.fcm_token = fcm_token
        if user_profiles:
            for user_profile in user_profiles:
                self.user_profiles.add(user_profile.user.pk)
        self.save()  ## possibly excessive saving trying to get this to work
        return self

When I go to create an instance of the UserProfile model, the pk is not automatically created. For example, I enter this into the interactive shell:

>>> user_profile = UserProfile.create(
    first_name = 'firstname',
    last_name = 'lastname',
    email = 'firstname.lastname@company.com',
    guid = "ed282e0c-4e9d-404b-ba70-8910ec7fe780"
)

Then when I access the primary key of the UserProfile, I get nothing:

>>> user_profile.pk

Interestingly, the pk IS created for the User model:

>>> user_profile.user.pk
3

When I call user_profile.save(), I get the following exception:

django.db.utils.IntegrityError: UNIQUE constraint failed: Interface_userprofile.user_id

What am I doing wrong? / What's going on?

Here's the full stack trace:

>>> user_profile.save()
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: Interface_userprofile.user_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Program Files\Python37\lib\site-packages\django\db\models\sql\compiler.py", line 1285, in execute_sql
    cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python37\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: Interface_userprofile.user_id

标签: pythondjangodjango-models

解决方案


我怀疑涉及instance.profile.save()您的混淆related_name='profile'以及在您的教程UserProfile中称为Profile.

在本教程中,.profile指的是模型Profile,但在您的情况下.profile指的userUserProfile.

会不会是你用profile的而不是user_profile?

我会尝试:

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.user_profile.save()

代替:

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

推荐阅读