简体   繁体   English

Django 迁移不适用于数据库

[英]Django migrations are not applied to database

A bit of backgound: We use docker container to run our django project and since there are a number of us working on the project we have put migrations folder into gitignore (whether this is right or wrong is a separate matter) to avoid merge conflicts.一点背景:我们使用 docker 容器来运行我们的 django 项目,由于我们有很多人在从事该项目,我们将migrations文件夹放入gitignore (这是对还是错是另一回事)以避免合并冲突。

Everytime makemigrations are run - it's always 001_initial.py as Docker copies the git project which has no migrations in it.每次运行makemigrations - 它总是001_initial.py因为 Docker 复制 git 项目,其中没有migrations It's up to migrate command to figure out which changes to apply to database.migrate命令决定哪些更改要应用于数据库。 It's becoming a common issue now that sometimes when there is a new field (or a field renamed) - although these changes are in myapp\migrations\001_initial.py - once migrate command is run, django thinks those changes are already in the database and it doesn't apply any migrations.现在它已成为一个常见问题,有时当有一个新字段(或重命名的字段)时 - 尽管这些更改位于myapp\migrations\001_initial.py - 一旦运行migrate命令,django 认为这些更改已经在数据库中并且它不应用任何迁移。 We are wiping the database and re-doing the migrations which works fine for development, but obviously it doesn't work for production.我们正在擦除数据库并重新进行迁移,这对开发工作很好,但显然它不适用于生产。 There are questions relating to this and people recommended to revert to the previous migrations which seems to reset and worked for us on some occasions - for example:有与此相关的问题,人们建议恢复到以前的迁移,这似乎在某些情况下重置并为我们工作 - 例如:

python manage.py migrate myapp zero

But it doesn't work always if there are relations in database with entirely new tables (so django complains that table doesn't exist) when trying to revert and partly because our migrations in docker container always start from 001_initial.py .但是,如果数据库中存在与全新表的关系(因此 django 抱怨该表不存在),则在尝试还原时它并不总是有效,部分原因是我们在 docker 容器中的迁移总是从001_initial.py开始。

Is this a django bug or are we not following any good practice hence the problem?这是一个 django 错误还是我们没有遵循任何好的做法因此问题? We are thinking of removing migrations folder from gitignore which enables reverting to older migrations, however we are not entirely sure whether this would fix the issue and besides django project is run in the container - so migrations files inside the container are not commited to git repo.我们正在考虑从gitignore中删除migrations文件夹,这可以恢复到旧迁移,但是我们不完全确定这是否会解决问题,并且除了 django 项目在容器中运行 - 因此容器内的migrations文件不会提交到 git repo .

I always migrate via the docker composer:我总是通过 docker 作曲家迁移:

docker-compose run <django-container> python manage.py migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM