Dockerized Django app trying to run the same migrations multiple times, leading to errors

Viewed 132

I have a Django app that is ran inside a Docker container. For my database I map a local directory to one in Docker with volumes. If I run the container with an empty database directory and build it, everything is fine. The database is created, migrations are applied etc. However, if I change anything in the models in Django and build again, I get the error that the migrations are already applied. For some reason, it is detecting the last 3-4 changes I made to the models and try to apply the migrations again. I mention that I am ignoring the database directory in .Dockerignore (because it was causing multiple access issues when passed to the server) and that in my docker-compose file I ran migrations with command: bash -c python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000". Does anyone have any idea what is happening?

web_1      |   ecommerce/migrations/0002_auto_20211211_2123.py
web_1      |     - Alter field name_service on service
web_1      |     - Alter field service on country
web_1      |     - Alter unique_together for service (1 constraint(s))
web_1      |     - Alter unique_together for country (1 constraint(s))
web_1      | System check identified some issues:
web_1      | 
web_1      | WARNINGS:
web_1      | ecommerce.Countries.service: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
web_1      |    HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
web_1      | Operations to perform:
web_1      |   Apply all migrations: admin, auth, contenttypes, sessions, ecommerce
web_1      | Running migrations:
db_1       | 2021-12-11 21:23:20.713 UTC [35] ERROR:  relation "Services_city_id_name_service_084de8e3_uniq" already exists
db_1       | 2021-12-11 21:23:20.713 UTC [35] STATEMENT:  ALTER TABLE "Services" ADD CONSTRAINT "Services_city_id_name_service_084de8e3_uniq" UNIQUE ("city_id", "name_service")
web_1      |   Applying ecommerce.0002_auto_20211211_2123...Traceback (most recent call last):
web_1      |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1      |     return self.cursor.execute(sql, params)
web_1      | psycopg2.errors.DuplicateTable: relation "Services_city_id_name_service_084de8e3_uniq" already exists

and my docker-compose is:


services:
  web:
    build: .
    command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    ports:
      - 8000:8000
    depends_on:
      - db
    env_file:
      - ./.secret.keys

  db:
    image: postgres:11
    volumes:
    - ./mydata/postgres_data:/var/lib/postgresql/data/
    ports:
    - "5431:5431"
    environment:
    <Postgres variables, I censored them as they are sensitive info>
   
0 Answers
Related