docker 的 Django 不会 运行 自定义应用程序的迁移
Django with docker doesn't run custom app's migrations
我在项目的根目录中创建了一个名为 bay
的自定义应用程序,结构如下所示 -
project_root/
- bay/
- migrations/
- __init__.py
- 0001_initial.py
- models/
- __init__.py
- document.py
- ... (other model files)
当我尝试从我的自定义应用程序模型之一保存新模型实例时,出现以下错误 -
postgres_1 | ERROR: relation "bay_brand" does not exist at character 13
postgres_1 | STATEMENT: INSERT INTO "bay_brand" ("name") VALUES ('xyz') RETURNING "bay_brand"."id"
因此,给定模型的迁移尚未 运行。所以,我尝试使用 -
进行迁移
sudo docker-compose -f dev.yml run django python manage.py migrate
这导致
Postgres is up - continuing...
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, sessions, sites, socialaccount, users
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying users.0001_initial... OK
Applying account.0001_initial... OK
Applying account.0002_email_max_length... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying sessions.0001_initial... OK
Applying sites.0001_initial... OK
Applying sites.0002_alter_domain_unique... OK
Applying sites.0003_set_site_domain_and_name... OK
Applying socialaccount.0001_initial... OK
Applying socialaccount.0002_token_max_lengths... OK
Applying socialaccount.0003_extra_data_default_dict... OK
Applying users.0002_auto_20170419_1104... OK
上面显然没有显示 bay.0001_initial
它应该 运行 的迁移。所以,我继续查看
的输出
sudo docker-compose -f dev.yml run django python manage.py makemigrations bay
这导致
Postgres is up - continuing...
Migrations for 'bay':
bay/migrations/0001_initial.py:
- Create model Brand
- Create model ...
- ...
如果我再次尝试 运行 migrate
命令,它会显示 No migrations to apply
。
我认为这是一个文件权限问题,在我的 migrations
文件夹中看到 __init__.py
文件和 __pycache__
文件夹上的锁定符号。最初忽略了它,因为它类似于我现有应用程序 migrations
文件夹中的那个,但这些迁移做得很好 运行。
删除了我的自定义应用程序中的 migrations
文件夹,运行 makemigrations
命令,然后 migrate
成功 -
Postgres is up - continuing...
Operations to perform:
Apply all migrations: account, admin, auth, bay, contenttypes, sessions, sites, socialaccount, users
Running migrations:
Applying bay.0001_initial... OK
我在项目的根目录中创建了一个名为 bay
的自定义应用程序,结构如下所示 -
project_root/
- bay/
- migrations/
- __init__.py
- 0001_initial.py
- models/
- __init__.py
- document.py
- ... (other model files)
当我尝试从我的自定义应用程序模型之一保存新模型实例时,出现以下错误 -
postgres_1 | ERROR: relation "bay_brand" does not exist at character 13
postgres_1 | STATEMENT: INSERT INTO "bay_brand" ("name") VALUES ('xyz') RETURNING "bay_brand"."id"
因此,给定模型的迁移尚未 运行。所以,我尝试使用 -
进行迁移sudo docker-compose -f dev.yml run django python manage.py migrate
这导致
Postgres is up - continuing...
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, sessions, sites, socialaccount, users
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying users.0001_initial... OK
Applying account.0001_initial... OK
Applying account.0002_email_max_length... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying sessions.0001_initial... OK
Applying sites.0001_initial... OK
Applying sites.0002_alter_domain_unique... OK
Applying sites.0003_set_site_domain_and_name... OK
Applying socialaccount.0001_initial... OK
Applying socialaccount.0002_token_max_lengths... OK
Applying socialaccount.0003_extra_data_default_dict... OK
Applying users.0002_auto_20170419_1104... OK
上面显然没有显示 bay.0001_initial
它应该 运行 的迁移。所以,我继续查看
sudo docker-compose -f dev.yml run django python manage.py makemigrations bay
这导致
Postgres is up - continuing...
Migrations for 'bay':
bay/migrations/0001_initial.py:
- Create model Brand
- Create model ...
- ...
如果我再次尝试 运行 migrate
命令,它会显示 No migrations to apply
。
我认为这是一个文件权限问题,在我的 migrations
文件夹中看到 __init__.py
文件和 __pycache__
文件夹上的锁定符号。最初忽略了它,因为它类似于我现有应用程序 migrations
文件夹中的那个,但这些迁移做得很好 运行。
删除了我的自定义应用程序中的 migrations
文件夹,运行 makemigrations
命令,然后 migrate
成功 -
Postgres is up - continuing...
Operations to perform:
Apply all migrations: account, admin, auth, bay, contenttypes, sessions, sites, socialaccount, users
Running migrations:
Applying bay.0001_initial... OK