python manage.py migrationが効かない時の対処法 [Django]

python manage.py migrationが効かない時の対処法 [Django]


donald chi / 2017-09-23 23:30

音声ファイル作成中...
Audio: Visited: 11364


背景

Djangoで開発する時、よく遭うな問題がいつくかあります。

例えば、

  1. modelを変更したけど、migrateできない
  2. 新しいjs, css, imgファイルを追加した時に、急に collectstaticが効かなくなる

など。

Djangoに対する理解が浅いせいか、私は頻繁に遭っています。

今回はそのうちのmigrateできない時の対処法をまとめます。


migrationの流れ

具体的な対処法をみて行く前にまずmigrationの流れからみていきます。

models.pyにモデルを定義した後に、それをmigrateして、データテーブルを作るまで 以下の 3 コマンド (或いは2コマンド)を順次的に実行することで実現します。

まず、

python manage.py makemigrations

このコマンドを実行すると、以下のようなものが作成されます。

djangoproject--
         |
            |----my_app
                          |
                          |----migrations
                                           |
                                           |----__init__.py
                                           |----0001_initial.py
                                           |----0002_xxxxx.py

タイトルが0001xxxファイルは初回目にmigrateした時のmodelの定義などが記録され、modelを削除、編集など作業をし、migrateするたびに、次の番号(0002xxx, 0003xxx)のマイグレーションファイルが作成されます。

次に、

python manage.py sqlmigrate

このコマンドを実行するとマイグレーションのためのSQLが作成されます。

実はこの部分はスキップすることができます。私の場合、いつもmakemigrationsとmigrate二つのコマンドでmigrationを実行しています

最後に、

python manage.py migrate

このコマンドを実行すると、マイグレーションが実行され、データベース上にテーブルが作成されます。


対処法

1. fake オプションの活用

よく使う方法として、以下の様にfakeオプションをつけてmigrateする方法があります。

fake オプションは、実際にはマイグレーションされていないが、されたようにシステムに見せつけ、次の操作をエラーなしに実行させる手法です。

あくまで個人の解釈です。ドキュメントには以下のように書かれています。

The --fake option tells Django to mark the migrations as having been applied or unapplied, but without actually running the SQL to change your database schema.

従って、migrateできない時は、一度 fakeオプションをつけて今の状態をスキップさせ、次にもう一度マイグレーションを試みることが考えられます。

python manage.py makemigrations --fake
python manage.py makemigrations
python manage.py migrate

2. 元のモデルを削除し、変わったモデル名で同じ構造のモデルを作成

この方法は、makemigrationsが効かない時に試せます。

もちろんその前に、pycacheの中にあるキャッシュファイルを削除して試すのも考えられます。


3. マイグレーションファイルを使って、今の操作の一歩前のマイグレーション状態に戻る

migrationの流れの説明で書いたように、毎回マイグレーションをすると、makemigrationsコマンドより作成されたマイグレーションファイルがmigrationsというフォルダーに残ります。

従って、うまくいかない時は、一歩前のマイグレーション状態に戻ることが可能です。

実行コマンド

python manage.py migrate my_app 0003_xxxxx(migration file name)

まとめ

これまで、

1. No migrations to apply
2. No Changes Detected

など、migrationができない時の対処法を三つまとめてみました。


参考文章

*関連ブログ

*Djangoドキュメント


0

0

Share with: Facebook Twitter Google+ LinkedIn Wechat Email Print

Comments: 6 件


Loans
mmiller0610@regiopost.trade

credit loans guaranteed approval <a href="https://creditloansguaranteedapproval.com">loans rates</a> bad credit loans guaranteed approval <a href=https://creditloansguaranteedapproval.com>cash lenders</a>

Loan Cash
trig43x@pochtar.men

payday loans today <a href="https://creditloansguaranteedapproval.com">bad credit loans guaranteed approval</a> bad credit loans direct lenders <a href=https://creditloansguaranteedapproval.com>poor credit loans</a>

Pay Day Loan
trig43x@rainmail.win

loans after bankruptcy <a href="https://creditloansguaranteedapproval.com/">direct online lenders</a> credit loans guaranteed approval <a href=https://creditloansguaranteedapproval.com/>bad credit loans guaranteed approval</a>

Payday Loan Online
larry@pochtar.men

loans for students <a href="https://creditloansguaranteedapproval.com/">poor credit loans</a> credit loans guaranteed approval <a href=https://creditloansguaranteedapproval.com/>bad credit loans</a>

Tony
tony.coombes85@gmail.com

Hi I have tried sending you a message from your site but I have gotten an auto response saying it was not delivered for some bizarre reason, so here goes again! Please acknowledge receipt. As I mentioned in my earlier message, I would like to contribute one of my guides on crypto-backed loans to your blog. I used to work for a payday loan company in the UK and so I have quite a good knowledge of the financial lending sector. I decided to write this guide because I feel that cryptocurrency will have a major impact on the payday loan industry in the next 5 to 10 years. I have tried to be as detailed as possible so you will find that the guide is fairly lengthy. Perhaps it would be a good idea for you to split it up into several blog posts or do as you see fit. I am terribly sorry but I did not have much time to find royalty free images. One company that I contacted whilst doing research and collecting references for this guide did give me permission to publish on of their banners that I have included in the g-drive folder. The entire guide is saved in a Word document inside my Google drive which you can access via this link https://drive.google.com/drive/folders/1TjO3WVH0tKuzotBh65w8jrJaCUfsjDdP?usp=sharing I hope your readers will enjoy reading my guide. I would be most grateful if you could send me a link to the guide once you have published it! I will endeavour to write a couple more articles as and when I get some more free time: it is hell at work after the festive period. Have a fab day. Regards Tony

Tony
tony.coombes85@gmail.com

Hi I have tried sending you a message from your site but I have gotten an auto response saying it was not delivered for some bizarre reason, so here goes again! Please acknowledge receipt. As I mentioned in my earlier message, I would like to contribute one of my guides on crypto-backed loans to your blog. I used to work for a payday loan company in the UK and so I have quite a good knowledge of the financial lending sector. I decided to write this guide because I feel that cryptocurrency will have a major impact on the payday loan industry in the next 5 to 10 years. I have tried to be as detailed as possible so you will find that the guide is fairly lengthy. Perhaps it would be a good idea for you to split it up into several blog posts or do as you see fit. I am terribly sorry but I did not have much time to find royalty free images. One company that I contacted whilst doing research and collecting references for this guide did give me permission to publish on of their banners that I have included in the g-drive folder. The entire guide is saved in a Word document inside my Google drive which you can access via this link https://drive.google.com/drive/folders/1TjO3WVH0tKuzotBh65w8jrJaCUfsjDdP?usp=sharing I hope your readers will enjoy reading my guide. I would be most grateful if you could send me a link to the guide once you have published it! I will endeavour to write a couple more articles as and when I get some more free time: it is hell at work after the festive period. Have a fab day. Regards Tony

Add Comment

Name:
Email:
Comment: