본문으로 바로가기
728x90
반응형

(1215, 'Cannot add foreign key constraint') 이 메시지가 이 케이스에서만 일어나는 상황은 아니기 때문에

메시지를 보고 적절히 대처하는 방법을 생각할 필요가 있다.

 

개요

django 프로젝트 스타일을 바꿔보려다가 발생한 상황이다.

 

Exception Message

Traceback (most recent call last):
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.IntegrityError: (1215, 'Cannot add foreign key constraint')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 230, in apply_migration
    migration_recorded = True
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 118, in __exit__
    self.execute(sql)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
    db.query(q)
  File "/Users/jako/github-src/django-hacksoft-style/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
    _mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')

 

상황

$ python manage.py makemigration
$ python manage.py migrate
django 프로젝트 초기 환경을 건드리고 위 명령대로 migrate 진행 중에 일어났다. python manage.py application 명령으로 앱 하나 만들고 그 밑에다가 다시 디렉터리를 만들어 app을 하나하나 추가하는 상황이었다. 즉 다음과 같은 구조이다
.
├── application <- python manage.py startapp application
│   ├── api <- New directory(app)
│   └── users <- New directory(app)
├── config
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings
│   ├── urls.py
│   └── wsgi.py
 

해결

직접 추가한 api와 users라는 디렉터리 밑에  migrations에 필요한 migrations 폴더를 빠뜨려 놓았으므로 생성해놓고 __init__.py 파일 생성을 추가 해줌으로써 해결했다.
├── application
│   ├── api
│   │   ├── apps.py
│   │   ├── migrations <- 추가해줌
│   │   │   └── __init__.py <- 추가해줌
│   │   └── urls.py
│   └── users
│       ├── __init__.py 
│       ├── api.py
│       ├── apps.py
│       ├── migrations <- 추가해줌
│       │   ├── 0001_initial.py
│       │   └── __init__.py <- 추가해줌
│       ├── models.py
│       ├── services.py
│       └── urls.py

 

 

 

728x90
반응형

'Frame Work > Django' 카테고리의 다른 글

uWSGI를 알아보자  (0) 2022.11.05
[Django] OneToOne Relation  (0) 2022.06.06
[Django] Session !  (0) 2022.05.06
[Django] Select && Prefetch Related !  (0) 2022.02.20
[Django] model 필드 순서 조정하기  (0) 2021.09.18