Django Primary Key Error Question!

Asked 1 years ago, Updated 1 years ago, 107 views

Code (models.py)

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    schoolid = models.CharField(max_length=5,unique=True,null=False,db_index=True)
    phone_number = models.CharField(max_length=11,unique=True,null=False,db_index=True)

    def __str__(self):
        return self.user.username

This is the content of the error when you migrate.

django.db.utils.OperationalError : table "mysite_userprofile" has more than one primary key

django python-2.7

2022-09-21 21:50

1 Answers

Read the error log.

It appears to be a problem that one or more primary keys currently exist in mysite_userprofile.

First of all, this problem seems to be more of a database problem than a problem with models.py, but it seems that the id value is twisted during the db modification process.

The same primary key should not exist in one database, just as two people cannot be given the same resident registration number. (In django, the id value is usually given as the primary key.)

To solve this problem, you can delete the database file and then migrate it again using the manage.py reset_db command using the library.

django extensions library


2022-09-21 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.