Django - Migrate command says that table exists after second makemigrations was executed

Vinu

I have one model with the relevant fields setup. I ran manage.py migrate then it created the a table in my database. I then ran manage.py makemigrations so that it could create an initial migration file with prefix '0001'. I added another field to my model and then ran manage.py makemigrations again, which created another migration file with prefix '0002'. When I run manage.py migrate now it still looks at the first migration file and so throws out an error that the 'Table already exists'. How can I make it only look at the latest migration file so that it adds the new column to the table? I'm using MySQL.

coolharsh55

This will never happen unless django detects (thinks) that the database has not been setup, and tries to initialise the tables with a schema.

Looks like your tables are already setup, but this is not known to django. That is why it tries to start applying the first migration - the table creation and schema is included in that.

You can use the --fake and --fake-initial options as per your specific problem, which tell django that the tables are already setup and ready, and to fake the migrations.

Useful links for more info:

django migrations - django documentation

django migrations, a primer - realpython

how to redo a migration after fake - stackoverflow

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Makemigrations/migrate in django

Django 2.2.4 - “No migrations to apply” when run migrate after makemigrations

Django 1.7 - "No migrations to apply" when run migrate after makemigrations

Django Makemigrations and Migrate keep repeating

Syncdb command in Django 1.6.5 does not create new model class in models.py - says table already exists

Django makemigrations show no changes detected after table rename in mysql

Migrate command create all tables on second DB - Django

Django: Error: Unknown command: 'makemigrations'

django 1.7 migrate gets error "table already exists"

How to create table with migrate after drop table from postgresql in django?

Detect whether code is being run in the context of migrate/makemigrations command

Command & binary exists but cannot be executed

Django Tutorial app, part 2. Why is missing - add field Question to Choice after makemigrations command?

Python Django migrate not picking up change from makemigrations

Will 'at' command be executed after the reboot?

django makemigrations fails after removing a class

Explaination about migrate command in django

Why django migrate command don't insert to django_migrations table

Laravel - php artisan migrate, table already exists

Makemigrations in Django

Batch command after loop is not executed

Second command on docker-compose is not executed

Table 'django_migrations' already exists after dropping database

Django makemigrations works, migrate fails with "django.db.utils.IntegrityError: NOT NULL constraint failed"

Python command prompt executed the second command before executing the first command

how to call second function after first is executed?

Django 1.10 - makemigrations command not detecting changes for unmanaged models

Prisma CLI command like Django's `makemigrations --check`

When i try to run makemigrations command in django it gave me an error