简体   繁体   English

IntegrityError(1452,'无法添加或更新子行:外键约束失败)

[英]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.我正在使用自定义用户 model (遵循教程)并且一切正常。

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.当我使用我在 /admin session 中使用 createsuperuser 创建的管理员用户登录时,我可以添加/删除/编辑任何我想要的东西。

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:这是我的用户 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?如何让不是超级管理员的用户真正能够在 /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.具体来说,我的模型使用表users_user来存储有关新添加用户的详细信息,并使用表auth_user来添加链接到我在数据库中的其他模型的外键。 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).事实上,我将用户详细信息从users_user表复制粘贴到auth_user表(因此有重复)的临时解决方法解决了问题(即没有返回任何错误)。

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.就我而言,我发现的其他解决方案(例如this )没有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.db.utils.IntegrityError: (1452, '无法添加或更新子行:外键约束失败 - django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails 错误1452(23000):无法添加或更新子行:外键约束失败我无法修复 - ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails That I can't fix Python 插入 MySQL:无法记录价格 1452 (23000):无法添加或更新子行:外键约束失败 - Python insert to MySQL : Failed to record Price 1452 (23000): Cannot add or update a child row: a foreign key constraint fails IntegrityError: (1451, '无法删除或更新父行:外键约束失败 (..)) - IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (..)) (pymysql.err.IntegrityError)(1451,'无法删除或更新父行:外键约束失败...') - (pymysql.err.IntegrityError) (1451, 'Cannot delete or update a parent row: a foreign key constraint fails…') in Flask Python Sqlalchemy mysql“无法添加或更新子行:外键约束失败” - Python Sqlalchemy mysql “Cannot add or update a child row: a foreign key constraint fails” 无法添加或更新子行:Django生成的MySQL表上的外键约束失败 - Cannot add or update a child row: a foreign key constraint fails on a Django generated MySQL table 在peewee中创建新模型实例时,如何修复“无法添加或更新子行:外键约束失败” - How to fix 'Cannot add or update a child row: a foreign key constraint fails' when creating new model instance in peewee 错误1452'无法添加或更新子行' - Error 1452 'Cannot add or update a child row' _mysql_exceptions.IntegrityError: (1215, '不能添加外键约束') - _mysql_exceptions.IntegrityError: (1215, 'Cannot add foreign key constraint')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM