2

I have a problem when migrating, cloning project and configuring on my computer but when I perform the first migration so that all the tables grow, I get the error. It is the first migration is a project in which the step dare has several tables but as I comment when making the first migration the error is created

Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying seguridad.0001_initial... OK
  Applying activos.0001_initial...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 93, in __exit__
    self.execute(sql)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/Users/gyanezc/Documents/Proyectos/corem-alher/envosx/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')

1 Answers1

0

I have experienced this type of problem in real life. The reason behind this problem is CHARSET in the database. You have to be concerned about it.

**Note:**latin1 != utf8 != utf8mb4

Let's check-

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

+--------------------------+-------------------+
| Variable_name            | Value             |
+--------------------------+-------------------+
| character_set_client     | utf8              |
| character_set_connection | utf8              |
| character_set_database   | latin1            |
| character_set_filesystem | binary            |
| character_set_results    | utf8              |
| character_set_server     | latin1            |
| character_set_system     | utf8              |
| collation_connection     | utf8_general_ci   |
| collation_database       | latin1_swedish_ci |
| collation_server         | latin1_swedish_ci |
+--------------------------+-------------------+

I will use utf8_general_ci as follows:

ALTER TABLE products_transaction CONVERT TO CHARACTER SET utf8 COLLATE 
utf8_general_ci;
mysql> SELECT CCSA.character_set_name 
-> FROM information_schema.`TABLES` T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
-> WHERE CCSA.collation_name = T.table_collation
-> AND T.table_schema = "ecom"
-> AND T.table_name = "ecom_products";

Then theDjango: django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint') issue is fixed.

On the other hand, you can solve this by using your database default charset: latin1 Default collation: latin1_swedish_ci