在django的/ accounts / signup /中的IntegrityError

时间:2015-03-31 07:39:00

标签: mysql django innodb ubuntu-14.04 myisam

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

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`))

1 个答案:

答案 0 :(得分:1)

从问题的评论来看,您的django模型和数据库架构看起来不同步。如果可以,只需删除数据库并重新创建即可。如果你有重要数据......

首先备份生产数据库

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

Django< = 1.6

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

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

Django> = 1.7

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

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

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

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