skip to Main Content

I am running the Django service in a docker container. Django has a Postgres database remote access from AWS RDS, and all this is working fine. But I am adding a new field (amount) in 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. price is new field here.
Whenever I start server and create migrations, new migrations are created ‘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?

2

Answers


  1. Chosen as BEST ANSWER

    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.

    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. i.e., git commit .


  2. Maybe you can consider deleting this file manually,I mean 0001_initial.py.
    Sometimes migration detection of django is not very effective.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search