본문 바로가기
Django11

Django Migrations

by 자동매매 2023. 4. 28.

Introduction to Django migration commands

Django 작업할 테이블을 만들거나 기존 테이블을 변경하기 위해 SQL 작성할 필요가 없습니다. 대신 Django 마이그레이션을 사용합니다.

Django 마이그레이션을 사용하면 명령줄을 통해 모델에 대한 변경 내용을 데이터베이스에 전파할  있습니다.

Django 모델에 대한 변경 내용을 기반으로 마이그레이션을 만들고 마이그레이션을 데이터베이스에 적용하기 위한 가지 명령을 제공합니다.

모델을 변경하고, 마이그레이션을 만들고, 변경 내용을 데이터베이스에 적용하는 프로세스는 다음과 같습니다.

 

The process for making changes to models, creating migrations, and applying the changes to the database is as follows:

  • First, define new models or make changes to existing models.
  • Second, make new migrations by running the makemigrations command.
  • Third, apply the changes from the models to the database by executing the migrate command.

Suppose that you define the Post models in the blog application like this:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Post(models.Model):
    title = models.CharField(max_length=120)
    content = models.TextField()
    published_at = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    

    def __str__(self):
        return self.title

 

you can create a new migration using the makemigrations command:

python manage.py makemigrations

 

 

백그라운드에서 명령은 migrations\0001_initial.py 파일을 만듭니다.

데이터베이스에 blog_post 테이블을  만들기 위해 Django 실행할 SQL 미리 보려면 sqlmigrate 명령을 사용합니다.

python manage.py sqlmigrate blog 0001

sqlmigrate 명령에서 blog 애플리케이션의 이름이고 0001 마이그레이션 번호입니다.

 

output:

BEGIN;
--
-- Create model Post
--
CREATE TABLE "blog_post" (
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, 
  "title" varchar(120) NOT NULL, 
  "content" text NOT NULL, 
  "published_at" datetime NOT NULL, 
  "author_id" integer NOT NULL REFERENCES "auth_user" ("id") 
   DEFERRABLE INITIALLY DEFERRED
);

CREATE INDEX "blog_post_author_id_dd7a8485" 
ON "blog_post" ("author_id");

COMMIT;

 

데이터베이스에 변경 내용을 적용하려면 migrate 명령을 실행합니다.

 

python manage.py migrate

 

프로젝트 마이그레이션 해당 상태를 나열하려면 showmigrations 명령을 사용합니다.

 

python manage.py showmigrations

 

output:

 

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
blog
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial

 

Summary

  • Use the makemigrations command to make migrations based on the changes that you made to the models.
  • Use the migrate command to apply changes from models to the database.
  • Use the sqlmigrate command to view the generated SQL based on the model.
  • Use the showmigrations command to list all migrations and their status in the project.

'Django11' 카테고리의 다른 글

Django Form 종류  (0) 2023.05.01
Django Flash Messages  (0) 2023.04.29
Django Models  (0) 2023.04.28
Django 배포  (0) 2023.04.14
Getting Started with Django  (0) 2023.04.08

댓글