본문 바로가기

Web

django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint') 에러

문제점


django에서 database에 테이블을 생성시 다음과 같은 에러가 난다.


Traceback (most recent call last):

  File "manage.py", line 10, in <module>

    execute_from_command_line(sys.argv)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line

    utility.execute()

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv

    self.execute(*args, **cmd_options)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute

    output = self.handle(*args, **options)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 25, in handle

    call_command("migrate", **options)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 120, in call_command

    return command.execute(*args, **defaults)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute

    output = self.handle(*args, **options)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 179, in handle

    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)

  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 317, in sync_apps

    cursor.execute(statement)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute

    return super(CursorDebugWrapper, self).execute(sql, params)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute

    return self.cursor.execute(sql, params)

  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__

    six.reraise(dj_exc_type, dj_exc_value, traceback)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute

    return self.cursor.execute(sql)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute

    return self.cursor.execute(query, args)

  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute

    self.errorhandler(self, exc, value)

  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler

    raise errorclass, errorvalue

django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')



환경


Django 1.8

ubuntu 14.04 LTS 64bit

MySQL 5.6.19


해결 및 대안


sqlite를 사용하면 에러가 뜨지 않지만, mysql을 사용하면 이런 에러가 나온다.

문제의 원인을 정확하게 이해하지는 못했지만, 최초에 syncdb나 migrate할때 만든 앱은 추가하지 말고 먼저 migrate를 해놓고 난뒤에 개발한 app을 settings에 추가하고 다시 migrate하면 에러가 발생하지 않는다..


예를들면,

최초에 아래처럼 default로 설정된 INSTALLED_APPS 상태 그대로 두고, migrate 한다.


INSTALLED_APPS = (

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

)


그런 다음에 개발한 App을 추가하고 다시 migrate하라는 의미..




참고

1) 1.9버젼 이상부터 syncdb 명령이 사라진다고 하니, migrate 명령을 사용할 것.

2) mysql은 5.5.5이상을 사용할 것.. 5.5.4버젼 까지는 MyISAM이라는 엔진을 사용하는데, 5.5.5이상부터는 InnoDB를 사용한다고 함. 그런데, MyISAM을 사용하는 5.5.4버젼의 경우에는 transactions 또는 foreign key constraints를 강제할 수 없다고 함..

(https://docs.djangoproject.com/en/1.8/ref/databases/#mysql-notes 참고)