makemigration and migrate

0

At first

We've continued to summarize what Django we're studying. I write it down instead of a memoir to myself. Once I understand the basic functions, I am planning to make a household accounting app or something.

What is database migration?

Indicates the ability to automatically create and manage database definitions for use by applications.

As a premise、、、

Models.py table is not created in the DB, but by executing makemigrate and migrate, the table is created in the DB.

makemigrate

Create a migration file to create a table in the DB. As a role, create a new migration for the model written in the models.py.

ターミナル
python manage.py makemigrations

In the case of large-scale development, if all the DBs are changed at once, there is a possibility that a problem will occur, so if only the corresponding application is changed, execute as follows.

ターミナル
python manage.py makemigrations (アプリ名)

migrate

Use to apply migrations or cancel applications. Based on the created migration file, create a table in the DB.

ターミナル
python manage.py migrate

You have XX (numeric) unapplied migration(s).

If you try to get the server up and it says You have XX(数字) unapplied migration(s)., you can do makemigration and migrate to update the tables in the database to resolve it.

Further reading

I have referred to the following.

Django Migration Summary https://qiita.com/okoppe8/items/c9f8372d5ac9a9679396 Getting Started with Python Using Django (Part 12) Migrating. https://tech.pjin.jp/blog/2017/06/29/python-primer-26-django-12/ Learn Notes: About Makemigrations and Migrate https://qiita.com/frosty/items/8c715a53d7920c9cd1eb migration https://docs.djangoproject.com/ja/3.2/topics/migrations/

Share:
0
9bo
Author by

9bo

いつか農業に関わりたいアプリ開発の人。

Updated on April 21, 2021