简体   繁体   中英

IntegrityError (1452, 'Cannot add or update a child row: a foreign key constraint fails)

I'm using a custom user model (following this tutorial) and all works well.

When I'm logged in with the admin user I created with createsuperuser in the /admin session I can add/remove/edit anything I want.

When I'm logged in with some other user, to which I've given staff and admin powers, I get this error whenever I want to add something to the database:

IntegrityError at /admin/users/user/13/change/ (or whichever action I'm doing)

(1452, 'Cannot add or update a child row: a foreign key constraint fails (`NMS_database`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')

[...]Exception Location:    /home/me/python_ve/lib/python3.8/site-packages/MySQLdb/connections.py in query, line 239

This is my user model:

class User(AbstractBaseUser):
    email = models.EmailField(verbose_name="email", unique=True, max_length=255)
    first_name = models.CharField(max_length=30, blank=True, null=True)
    surname = models.CharField(max_length=30, blank=True, null=True)

    additional = models.BooleanField(default=False)

    individual = models.BooleanField(default=True)
    active = models.BooleanField(default=True) #can they login?
    staff = models.BooleanField(default=False) #staff user non superuser
    admin = models.BooleanField(default=False) #superuser
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email' # default identifier for that user which will used for logging in

    #email (USERNAME_FIELD in this case) and password are required by default

    REQUIRED_FIELDS = ['first_name', 'surname']

    def __str__(self):
        return "%s %s" % (self.first_name, self.surname)

    def get_full_name(self):
        return self.first_name

    def has_perm(self, perm, obj=None):
        return self.admin

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin


# hook in the New Manager to our Model
    objects = UserManager()

How can I get users who are not superadmin to be actually able to do things in /admin?

This was due to a general point regarding custom user models, ie that they cannot be introduced half-way through.

Specifically, my models were using a table users_user for storing details about new added users, and the table auth_user for adding foreign keys linked to other models I have in the database. In fact, my temporary workaround of copy-pasting the user's details from the users_user table to auth_user table (thus having a duplicate) solved the problem (ie didn't return any error).

The 'easy' solution here is to delete the database entirely and recreate it. Other solutions I've found (eg this ) did not help, in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM