0

I am running a Django project inside a Docker container, and I am facing an issue with migrations after adding a new model to models.py.

When I try to apply the migrations, the system looks for the old migrations, but since I rebuild the app via Docker, those migrations are missing. When I try to force Django to ignore the old migrations, it attempts to create tables that already exist, and the process fails because of that.

If I allow it to run the migrations normally, it tries to re-apply the migrations that have already been executed, which leads to errors.

I need a way to: • Skip old migrations during the rebuild. • Ensure the new tables are created without conflicting with existing ones. • Prevent Django from reapplying migrations that are already done.

How can I resolve this migration issue in Docker? Any help would be appreciated!

Here’s what I have in my Dockerfile (now there is that problem with creating existing tables):

FROM python:3.11

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

# Vyčistíme migrace a vytvoříme nové při buildu image
RUN rm -rf api/migrations/* && \
    touch api/migrations/__init__.py 

# Instalace netcat pro kontrolu dostupnosti databáze
RUN apt-get update && apt-get install -y netcat-traditional

# Command který se spustí při startu kontejneru
CMD bash -c '\
    while ! nc -z db 5432; do \
        echo "Waiting for database..." && \
        sleep 1; \
    done && \
    echo "Database is up!" && \
    python manage.py makemigrations api && \
    python manage.py migrate && \
    python manage.py runserver 0.0.0.0:8000'
2
  • 1
    Your migration files should persist between deployments, and those are stored in your DB as names when they are run. It looks like you are removing your migration files, which would rename them when you makemigrations so it would run them again. Remove this: RUN rm -rf api/migrations/* Commented Jan 2 at 20:10
  • You can use docker-compose.yaml file to make a volume for persistent storage of all your code, including migrations, syncing your local filesystem with Docker filesystem. I found this tutorial most helpful for this task: Django, Docker, and PostgreSQL Tutorial Commented Jan 14 at 13:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.