model.pyの変更をDBに反映させる

ぶっつけ本番でDBを触るのはこわいので、検証をやってみました。
今回は、model.pyの変更を、データベースに反映させてみます。
最初のmodel.pyはこんな感じ。

```model.py
from django.db import models


class Person(models.Model):
    name = models.CharField("名前", max_length=100)
    age = models.IntegerField("年齢")

    def __str__(self):
        return self.name


class Team(models.Model):
    full_name = models.CharField("チーム名", max_length=100) ⬅️full_nameをnameに変更
    members = models.ManyToManyField("Person", through="PersonTeamRelation", through_fields=("team", "person"))

    def __str__(self):
        return self.name

class PersonTeamRelation(models.Model):
    team = models.ForeignKey("Team", models.DO_NOTHING)
    person = models.ForeignKey("Person", models.DO_NOTHING)
    joined_date = models.DateField(auto_now=False, auto_now_add=False)

```

Team.full_nameをTeam.nameに変更してmakemigrations, migrateしてみると。

```bash.sh
Did you rename team.full_name to team.name (a CharField)? [y/N] y ⬅️本当にいいの？と聞かれる
Migrations for 'project':
  project/migrations/0002_auto_20200206_1441.py
    - Rename field full_name on team to name　⬅️どう変わったかの確認
>>> python manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, project, sessions　
Running migrations:
  Applying project.0002_auto_20200206_1441... OK　⬅️projectに反映されたよ
>>> python manage.py showmigrations　⬅️これでマイグレーションの履歴がみれる
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
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
project
 [X] 0001_initial
 [X] 0002_auto_20200206_1441　⬅️今回やったマイグレーション
sessions
 [X] 0001_initial
```

上手くいったので、思い切ってテーブルを増やしてみた。

```model.py
from django.db import models


class User(models.Model):
    name = models.CharField("名前", max_length=100)
    age = models.IntegerField("年齢")

    def __str__(self):
        return self.name

class TeamProfile(models.Model):
    user = models.OneToOneField(User, related_name="profile", on_delete=models.CASCADE)
    score = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now=True)


class Team(models.Model):
    name = models.CharField("チーム名", max_length=100)
    members = models.ManyToManyField("User", through="UserTeamRelation", through_fields=("team", "user"))

    def __str__(self):
        return self.name

class UserTeamRelation(models.Model):
    team = models.ForeignKey("Team", models.DO_NOTHING)
    user = models.ForeignKey("User", models.DO_NOTHING)
    joined_date = models.DateField(auto_now=False, auto_now_add=False)

```

```bash.sh
>>> python manage.py makemigrations
Did you rename the project.Person model to User? [y/N] y
Did you rename the project.PersonTeamRelation model to UserTeamRelation? [y/N] y
Did you rename userteamrelation.person to userteamrelation.user (a ForeignKey)? [y/N] y
Migrations for 'project':
  project/migrations/0003_auto_20200206_1501.py
    - Rename model PersonTeamRelation to UserTeamRelation
    - Rename field person on userteamrelation to user
    - Rename model Person to User
    - Create model TeamProfile
>>> python manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, project, sessions
Running migrations:
  Applying project.0003_auto_20200206_1501... OK
>>> python manage.py makemigrations
Migrations for 'project':
  project/migrations/0004_auto_20200206_1503.py
    - Alter field score on teamprofile

```