Migrations detected wherease makemigrations/migrate already done (and database changed already applied)

stack: Django/Docker/Postgresql

I have made some changes in database models last month and deployed in preprod.

- remove fields 
- add fields
- alter one field constraint

All seems to be correct, changes were applied and app was running.

Yesterday, I've made some minor changes and re-deployed but when I re-build my project, new migrations are detected. These migrations are exactly the same as above. And migrate failed because trying to remove a field that did not exist anymore.

Django app update procedure:

- sudo docker-compose -f docker-compose.preprod.yml down -v
- git pull
- sudo docker-compose -f docker-compose.preprod.yml up -d --build --remove-orphans #<= error raise
- sudo docker-compose -f docker-compose.preprod.yml up

entrypoint.sh

#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $SQL_HOST $SQL_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

python manage.py makemigrations --noinput
python manage.py migrate

exec "$@"

Your entrypoint must absolutely not have makemigrations in it. If you have been running with this in production, you may be screwed (i.e. you'll have migrations in your production database that are to be found nowhere else).

makemigrations must only be run at development time, and those migrations must be committed to source control.

Back to Top