gpt4 book ai didi

mysql - IntegrityError at/accounts/signup/in django

转载 作者:行者123 更新时间:2023-11-29 00:01:59 25 4
gpt4 key购买 nike

我刚刚在 ubuntu 14.04 服务器上使用 apache 部署了我的 django 站点。正在访问该站点。但是当我以用户身份注册到其中时,它会创建用户但无法创建与我的用户处于一对一关系的用户配置文件。另外,我正在使用自定义用户模型。它在注册时抛出的错误是:

Cannot add or update a child row: a foreign key constraint fails (`ilog_prod_db`.`myuserprofile_myuserprofile`, CONSTRAINT `myuserprofile_myuserp_myuser_id_6ad6f704425ed359_fk_auth_user_id` FOREIGN KEY (`myuser_id`) REFERENCES `auth_user` (`id`))

重点是,在我的开发机器 (windows7) 上,该网站使用相同的代码运行得非常好。此外,数据库的设置完全相同,即在两个地方我都使用 InnoDB 作为数据库的数据库引擎。

可能是什么问题。任何指针将不胜感激。谢谢。

回溯:

Environment:


Request Method: POST
Request URL: http://www.industrylogger.com/accounts/signup/

Django Version: 1.7.2
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'enterprise',
'accounts',
'nodes',
'message',
'myuserprofile',
'enterprise_profile',
'activities',
'search',
'imagekit')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/sp/webapps/ilog_dev/accounts/views.py" in signup
24. password=password,)
File "/home/sp/webapps/ilog_dev/accounts/models.py" in create_myuser
93. user.save(using=self._db)
File "/home/sp/webapps/ilog_dev/accounts/models.py" in save
152. super(MyUser, self).save(*args, **kwargs)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/base.py" in save
589. force_update=force_update, update_fields=update_fields)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/base.py" in save_base
626. update_fields=update_fields, raw=raw, using=using)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/dispatch/dispatcher.py" in send
198. response = receiver(signal=self, sender=sender, **named)
File "/home/sp/webapps/ilog_dev/myuserprofile/models.py" in create_user_profile
121. MyUserProfile.objects.create(myuser=instance)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/query.py" in create
372. obj.save(force_insert=True, using=self.db)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/base.py" in save
589. force_update=force_update, update_fields=update_fields)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/base.py" in save_base
617. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/base.py" in _save_table
698. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/base.py" in _do_insert
731. using=using, raw=raw)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
92. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/query.py" in _insert
921. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
920. cursor.execute(sql, params)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/mysql/connector/django/base.py" in execute
135. return self._execute_wrapper(self.cursor.execute, query, args)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/mysql/connector/django/base.py" in _execute_wrapper
121. utils.IntegrityError(err.msg), sys.exc_info()[2])
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/django/utils/six.py" in reraise
658. raise value.with_traceback(tb)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/mysql/connector/django/base.py" in _execute_wrapper
115. return method(query, args)
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/mysql/connector/cursor.py" in execute
507. self._handle_result(self._connection.cmd_query(stmt))
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/mysql/connector/connection.py" in cmd_query
722. result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/sp/webapps/myenv/lib/python3.4/site-packages/mysql/connector/connection.py" in _handle_result
640. raise errors.get_exception(packet)

Exception Type: IntegrityError at /accounts/signup/
Exception Value: Cannot add or update a child row: a foreign key constraint fails (`ilog_prod_db`.`myuserprofile_myuserprofile`, CONSTRAINT `myuserprofile_myuserp_myuser_id_6ad6f704425ed359_fk_auth_user_id` FOREIGN KEY (`myuser_id`) REFERENCES `auth_user` (`id`))

最佳答案

从问题的评论来看,您的 Django 模型和数据库模式似乎不同步。如果可以,只需删除数据库并重新创建。如果您有重要数据...

首先备份您的生产数据库

根据您使用的 django 版本,数据库同步的行为会有所不同。

Django <= 1.6

python manage.py syncdb 命令可用于将任何缺失的模式信息添加到数据库中。如果您的问题源于您向模型添加了新表或字段/约束已被删除,这可能会修复架构。

它不会删除任何旧字段或迁移任何数据,因此您之后很可能需要进行一些手动编辑。

Django >= 1.7

1.7 引入了新的 python manage.py migrate ( Docs ) 命令,它更加智能。随着模型的更改,将创建迁移。当旧数据库需要与新模型内联时,迁移将应用于数据库。

这更加灵活,并允许根据需要升级/降级数据库版本。

缺点是您需要随时进行迁移(或之后手动创建)。有关详细信息,请参阅文档

在任何一种情况下,如果模型太不同步或者您没有适当的迁移,您将需要做一些手动工作以使数据进入 django 所需的状态。

关于mysql - IntegrityError at/accounts/signup/in django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29363150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com