[英]Why am I unable to add new field in Django model? I am using docker container
I am running the Django service in a docker container.我在 docker 容器中运行 Django 服务。 Django has a Postgres database remote access from AWS RDS, and all this is working fine.
Django 从 AWS RDS 远程访问 Postgres 数据库,一切正常。 But I am adding a new field (amount) in Model.
但我在 Model 中添加了一个新字段(金额) 。
class MyModel(models.Model):
model_id = models.UUIDField(default=uuid.uuid4)
model2 = models.ForeignKey(MyModel2, on_delete=models.CASCADE, related_name='mymodels')
amount = models.FloatField(default=0) # this is new field
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
MyModel table already exists in the database.数据库中已存在MyModel表。 price is new field here.
价格是这里的新领域。 Whenever I start server and create migrations, new migrations are created 'accounts/migrations/0001_initial.py'.
每当我启动服务器并创建迁移时,都会创建新的迁移“accounts/migrations/0001_initial.py”。
docker-compose build
docker-compose up -d
docker-compose exec djangoservice bash
~\ python manage.py makemigrations
~\ python manage.py migrate
after this process, no changes in my database table.在此过程之后,我的数据库表没有任何变化。 Can someone tell me what am I doing wrong?
有人可以告诉我我做错了什么吗? How can I add new fields in my exesting table?
如何在现有表中添加新字段?
Maybe you can consider deleting this file manually,I mean 0001_initial.py.也许你可以考虑手动删除这个文件,我的意思是 0001_initial.py。 Sometimes migration detection of django is not very effective.
有时 django 的迁移检测不是很有效。
To get reflect all changes in the local repo that occurred in the docker container's code repository, I added a volume to sync the container with the local repo.为了反映 docker 容器的代码存储库中发生的本地存储库中的所有更改,我添加了一个卷以将容器与本地存储库同步。
services:
web:
build: ./app
ports:
- "8000:8000"
volumes:
- ./app:/home/app_dir # this volume
command: gunicorn djangoproject.wsgi:application --workers=2 --bind 0.0.0.0:8000
expose:
- 8000
Now build and up the container;现在构建容器;
docker-compose exec web bash
/app$ cd /home/app_dir
/app$ python manage.py makemigrations
After executing these commands you'll see all migration files will be added to local migration folders.执行这些命令后,您会看到所有迁移文件都将添加到本地迁移文件夹中。 Now commit these files to the version control repo.
现在将这些文件提交到版本控制仓库。 ie, git commit.
即,git 提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.