LoginSignup
4
6

More than 3 years have passed since last update.

【Django】Modelを作成後makemigrationsを実行してもNo changes detectedになる

Posted at

現象

新規にmodelを作成後、makemigrationsを実行しても下記のような出力となり、マイグレーションファイルが作成されない現象に遭遇したので、その対応策をメモ。

# modelを新規作成後下記コマンドを実行
$ python manage.py makemigrations
No changes detected

フォルダの構造

※詳細は割愛

app
├─migrations
├─models
│  ├─__init__.py
│  ├─accounts.py
│  └─users.py    # 今回新しく追加したmodel
└─views
   ├─__init__.py
   └─account.py

原因

どうやらmodelsフォルダに分割してmodelを作成している場合は_init_.pyに新規で作成したmodelをインポートしないと認識してくれないらしい。

もともとの_init_.pyの中身

app/models/__init__.py
from accounts.py import AccountsModel

ここに新しく追加したusers.pyを追加する。

app/models/__init__.py
from accounts.py import AccountsModel
from users.py import UsersModel # 新規に追加

再度実行

$ python manage.py makemigrations
Migrations for 'app':
  app\migrations\0002_users.py
    - Create model UsersModel

無事に作成できました!

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6