我在项目的根目录中创建了一个名为 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
命令,它会显示没有要应用的迁移
。
最佳答案
我相信这是一个文件权限问题,在我的迁移
中的__init__.py
文件和__pycache__
文件夹上看到了锁定符号文件夹。最初忽略了它,因为它与我现有应用的 migrations
文件夹中的类似,但这些迁移确实运行良好。
删除了自定义应用中的 migrations
文件夹,运行 makemigrations
命令,然后成功迁移
-
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
关于python - 带有 docker 的 Django 不运行自定义应用程序的迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43489756/