Django's models.py is linked to the database compared to other views.py, so once you mess with it, it seems to be very troublesome, but this time the solution has been settled, so be polite in your own way I tried to describe it in. If it's hard to understand, I'm sorry.
Python==3.6.1 Django==2.2.1 djangorestframework==3.11.0
"Migration of models.py is completed! Oh, I found a typo in the variable name ... Rewrite and migrate (` $ python manage.py makemigratios → $ python manage.py migrate```), error ... what Even if I do it, an error ... What should I do? " Here's how to solve this.
Type a command
$ python manage.py makemigrations --empty <app name>
Migrations for '<app name>':
<app name>/migrations/0012_auto_20200610_0000.py
#0012_auto_20200610_0000.py
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<app name>', '0011_auto_20200610_1111'),
]
operations = [
]
#0012_auto_20200610_0000.py
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<app name>', '0011_auto_20200610_1111'),
]
operations = [
migrations.RenameField(
model_name='<The model name you want to change inside the app>',
old_name='image1Discription', #Example: Variable name you want to change
new_name='image1Description' #Example: Variable name after change
)
]
#0012_auto_20200610_0000.py
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<app name>', '0011_auto_20200610_1111'),
]
operations = [
migrations.RenameField(
model_name='<The model name you want to change inside the app>',
old_name='image1Discription', #Example: Variable name you want to change
new_name='image1Description' #Example: Variable name after change
),
migrations.RenameField(
model_name='<The model name you want to change inside the app>',
old_name='image2Discription', #Example: Variable name you want to change
new_name='image2Description' #Example: Variable name after change
)
]
Don't forget to specify the DB of settings.py locally.
$ python manage.py migrate
Running migrations:
Applying realestatedb.0012_auto_20200610_0000... OK
You can now change your local db (such as postgre). Since models.py is related to the behavior of apps other than DB, even if you run it now, you will get a 503 error, but if you write changes to models.py in a later process, the error will be fixed.
Don't forget to specify the DB of settings.py remotely.
$ git add .
$ git commit -m "db revise"
$ git push heroku master
(Wait)
$ heroku run python manage.py migrate
Running migrations:
Applying realestatedb.0012_auto_20200610_0000... OK
With this, the remote DB is OK for the time being.
$ git add .
$ git commit -m "db revise"
$ git push heroku master
(Wait)
If you check the operation and it is OK, you are done. Just because you messed with models.py, the migration is already done, so you don't need to migrate further.
"Migration of models.py is complete! Oh, I thought it was this variable Int, but it's Datetime, not Int ... I made a mistake ... Rewrite and migrate (` $ python manage.py makemigratios → $ python In manage.py migrate```), an error ... an error no matter what ... what should I do? " Here's how to solve this.
$ python manage.py makemigrations --empty realestatedb
Migrations for 'realestatedb':
realestatedb/migrations/0012_auto_20200610_0000.py
#0012_auto_20200610_0000.py
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<app name>', '0011_auto_20200610_1111'),
]
operations = [
]
from django.db import migrations, models
Please note that an error will occur if you do not add the import of ``` models` `` to.
#### **`#0012_auto_20200610_0000.py`**
```py
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('<app name>', '0011_auto_20200610_1111'),
]
operations = [
migrations.AlterField(
model_name='<The model name you want to change inside the app>',
name='FeePayDate', #Example: Variable name you want to change
field=models.DateTimeField(blank=True, null=True), #Example: Data type name after change
),
]
Don't forget to specify the DB of settings.py locally.
$ python manage.py migrate
Running migrations:
Applying realestatedb.0012_auto_20200610_0000... OK
You can now change your local db (such as postgre). Since models.py is related to the behavior of apps other than DB, even if you run it now, you will get a 503 error, but if you write changes to models.py in a later process, the error will be fixed.
Don't forget to specify the DB of settings.py remotely.
$ git add .
$ git commit -m "db field revise"
$ git push heroku master
(Wait)
$ heroku run python manage.py migrate
Running migrations:
Applying realestatedb.0012_auto_20200610_0000... OK
With this, the remote DB is OK for the time being.
$ git add .
$ git commit -m "db field revise"
$ git push heroku master
(Wait)
If you check the operation and it is OK, you are done. Just because you messed with models.py, the migration is already done, so you don't need to migrate further.
Rename table columns in Django3 https://qiita.com/holly0819/items/41f01096f59416b0d52b
Learning notes for the migrations feature in the Django framework (3) https://qiita.com/pumbaacave/items/8b6f8d96ddc7cc112827
Change column type with django migrations https://stackoverflow.com/questions/27385118/change-column-type-with-django-migrations
Recommended Posts