简体   繁体   English

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

[英]django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails

from django.db import models
from django.contrib.auth.models import User

Previously my models was like this.以前我的模型是这样的。

class Login(models.Model):
    pid              = models.AutoField(primary_key=True)
    custom_username  = models.BooleanField(default=False)
    tnx_hash         = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return self.username

Then i changed to like this to inherit from base admin user model.然后我改成这样继承基本管理员用户 model。

class Login(User):
    pid              = models.AutoField(primary_key=True)
    custom_username  = models.BooleanField(default=False)
    tnx_hash         = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return self.username

Now when i am running makemigrations and migrate getting below error.现在,当我运行 makemigrations 并迁移时,出现以下错误。

        Operations to perform:
        Apply all migrations: admin, api, auth, authtoken, contenttypes, sessions
        Running migrations:
        Applying api.0278_auto_20220318_1210...Traceback (most recent call last):
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
            return self.cursor.execute(sql, params)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 73, in execute
            return self.cursor.execute(query, args)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
            res = self._query(query)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
            db.query(q)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/MySQLdb/connections.py", line 259, in query
            _mysql.connection.query(self, query)
        MySQLdb._exceptions.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`baby`.`#sql-16a7_6a`, CONSTRAINT `api_login_user_ptr_id_7f748092_fk_auth_user_id` FOREIGN KEY (`user_ptr_id`) REFERENCES `auth_user` (`id`))')

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

        Traceback (most recent call last):
        File "manage.py", line 22, in <module>
            main()
        File "manage.py", line 18, in main
            execute_from_command_line(sys.argv)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
            utility.execute()
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
            self.fetch_command(subcommand).run_from_argv(self.argv)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
            self.execute(*args, **cmd_options)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
            output = self.handle(*args, **options)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
            res = handle_func(*args, **kwargs)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
            post_migrate_state = executor.migrate(
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
            state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
            state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
            state = migration.apply(state, schema_editor)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/migrations/migration.py", line 126, in apply
            operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 104, in database_forwards
            schema_editor.add_field(
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/mysql/schema.py", line 98, in add_field
            super().add_field(model, field)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 522, in add_field
            self.execute(sql, params)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
            cursor.execute(sql, params)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
            return super().execute(sql, params)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
            return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
            return executor(sql, params, many, context)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
            return self.cursor.execute(sql, params)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
            raise dj_exc_value.with_traceback(traceback) from exc_value
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
            return self.cursor.execute(sql, params)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 73, in execute
            return self.cursor.execute(query, args)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
            res = self._query(query)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
            db.query(q)
        File "/Users/soubhagyapradhan/Desktop/upwork/polyverse/polyverse_api/env/lib/python3.8/site-packages/MySQLdb/connections.py", line 259, in query
            _mysql.connection.query(self, query)
        django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`baby`.`#sql-16a7_6a`, CONSTRAINT `api_login_user_ptr_id_7f748092_fk_auth_user_id` FOREIGN KEY (`user_ptr_id`) REFERENCES `auth_user` (`id`))')

Please take a look.请看一下。 How can i solve this problem safely.我怎样才能安全地解决这个问题。 Because i have data in my database and login table is foreign key for many tables.因为我的数据库中有数据,而登录表是许多表的外键。

    ERRORS:
    api.Login.groups: (fields.E304) Reverse accessor for 'api.Login.groups' clashes with reverse accessor for 'auth.User.groups'.
        HINT: Add or change a related_name argument to the definition for 'api.Login.groups' or 'auth.User.groups'.
    api.Login.user_permissions: (fields.E304) Reverse accessor for 'api.Login.user_permissions' clashes with reverse accessor for 'auth.User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'api.Login.user_permissions' or 'auth.User.user_permissions'.
    auth.User.groups: (fields.E304) Reverse accessor for 'auth.User.groups' clashes with reverse accessor for 'api.Login.groups'.
        HINT: Add or change a related_name argument to the definition for 'auth.User.groups' or 'api.Login.groups'.
    auth.User.user_permissions: (fields.E304) Reverse accessor for 'auth.User.user_permissions' clashes with reverse accessor for 'api.Login.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'auth.User.user_permissions' or 'api.Login.user_permissions'.

Inheriting from the User model wont work, since it has its own primary key id .User model 继承是行不通的,因为它有自己的主键id You have a primary key defined pid within your Login model. Instead, if you only want to extend the Login model with the User fields, you could inherit from the AbstractUser class instead.您在Login model 中有一个主键定义的pid 。相反,如果您只想使用User字段扩展Login model,则可以从AbstractUser class 继承。

If you want to switch to the Login model as the default User Model, you would have to migrate the data from the auth_user table to the api_login table somehow, either using a data migration or SQL and also set AUTH_USER_MODEL = "api.Login"如果要切换到Login model 作为默认用户 Model,则api_login auth_user ,使用数据迁移或 SQL 并设置AUTH_USER_MODEL = "api.Login"

暂无
暂无

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

相关问题 IntegrityError(1452,'无法添加或更新子行:外键约束失败) - IntegrityError (1452, 'Cannot add or update a child row: a foreign key constraint fails) django.db.utils.IntegrityError: FOREIGN KEY 约束在 django 中失败 - django.db.utils.IntegrityError: FOREIGN KEY constraint failed in django django.db.utils.IntegrityError: FOREIGN KEY 约束失败 - django.db.utils.IntegrityError: FOREIGN KEY constraint failed django.db.utils.IntegrityError:插入或更新表“authtoken_token”违反外键约束 - django.db.utils.IntegrityError: insert or update on table “authtoken_token” violates foreign key constraint 错误1452(23000):无法添加或更新子行:外键约束失败我无法修复 - ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails That I can't fix django.db.utils.IntegrityError: FOREIGN KEY 约束在通过 Selenium 和 Python Django 执行 LiveServerTestCases 时失败 - django.db.utils.IntegrityError: FOREIGN KEY constraint failed while executing LiveServerTestCases through Selenium and Python Django Django错误django.db.utils.IntegrityError:FOREIGN KEY约束失败 - Django Error django.db.utils.IntegrityError: FOREIGN KEY constraint failed django.db.utils.IntegrityError:表“Event_event”中主键为“1”的行具有无效的外键: - django.db.utils.IntegrityError: The row in table 'Event_event' with primary key '1' has an invalid foreign key: 测试失败,出现“ django.db.utils.IntegrityError” - Test fails with `django.db.utils.IntegrityError` 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM