IntegrityError at /accounts/signup/ in django
Question: I have just deployed my django site using apache on an ubuntu 14.04 server.
The site is being accessed. But when i signup as a user into it, it creates
the user but is unable to create the user profile which is in a one to one
relationship with myuser. Also, I am using a custom user model. the error it
throws on signup is:

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

The point is that, on my dev machine(windows7), the website is working
perfectly fine with the same code. Also, the settings of database is exactly
the same, ie in both places i have used InnoDB as the database engine for the
database.

What can be the problem. Any pointers would be appreciated. Thanks.

Traceback:

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


Answer: From the comments on the question, it looks like your django model and
database schema are out of sync. If you can, just delete the database and
recreate. If you have important data in there...

**First make a backup of your production database**

Depending on which version of django you're using, database synchronisation
behaves differently.

### Django <= 1.6

The `python manage.py syncdb` command can be used to add any missing schema
information to your database. If your problem stems from the fact that you've
added new tables or fields to your model / that constraints have been deleted,
this might fix the schema.

It will not remove any old fields or migrate any data, so there's a good
chance you'll need to do some manual editing afterwards.

### Django >= 1.7

1.7 introduced the new `python manage.py migrate`
([Docs](https://docs.djangoproject.com/en/1.7/topics/migrations/)) command
which is more intelligent. As your model changes, migrations are created. When
an old database needs to be brought inline with a new model, the migrations
are applied to the database.

This is far more flexible and allows database versions to be
upgraded/downgraded as required.

The downside is that you need to have made migrations as you go (or create
them manually afterwards). See the documentation for more information

In either case, if the model is too far out of sync or you don't have the
appropriate migrations, you're going to need to do some manual work to get the
data into the state required by django.

