Search code examples
djangodocker-composedjango-migrations

makemigrations in Django (under docker-compose) doesn't stored files of migrations, but performs database changes


Today my colleague suggested deleting the migration files in order to create them again. When I went to this action, I saw that in my application's migrations folder there was only init.py At the same time, the actions necessary with the database (in particular, the creation of additional tables for models) were previously performed.

I'm working at this project about week, and performed commands below more than one time, sure! But never saw that no any files stored into migrations folder.

This is my command to run makemigrations: docker-compose exec web python manage.py makemigrations

This is the command to perform migrations: docker-compose exec web python manage.py migrate --noinput

This is my docker-compose.yml:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    volumes:
      - ./static/:/app/static/
      - ./media/:/app/media/
    command: >
      sh -c "gunicorn conf.wsgi:application --bind 0:8000"
    depends_on:
      - db
    ports:
      - "8000:8000"
    env_file:
      - .env
    environment:
      - DEBUG=True
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"


  db:
    image: postgres:latest
    environment:
      POSTGRES_DB: $DB_NAME
      POSTGRES_USER: $DB_USER
      POSTGRES_PASSWORD: $DB_PASS

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: pgadmin4@pgadmin.org
      PGADMIN_DEFAULT_PASSWORD: postgres
    ports:
      - "5050:80"

volumes:
  postgres_data:
  static_volume:
  ssl_volume:

I tried to restart containers, docker. Restarted IDE (PyCharm) some times also.


Solution

  • Ideally you store the migration files in the github repo. What happens if you make a mistake and forget to update your local database? Also the migration files will be removed after every deployment as it not mounted to a volume. This will not allow you to revert to a previous migration if you make a mistake somewhere.

    Rule of thumb is to have the migration files in the github repo and on your server you only run python manage.py migrate to ensure that your local environment is as closely matching your production as possible.