1

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.

Vinu
  • 109
  • 1
  • 14

1 Answers1

2

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

Community
  • 1
  • 1
coolharsh55
  • 1,179
  • 4
  • 12
  • 27
  • Hi kicker86. I decided to start from fresh just to confirm what I have been experiencing. I dropped the database and created a new one; then I deleted all the files in the migrations folder in my app folder. I then ran `manage.py migrate` which worked as expected and then I ran `manage.py makemigrations`. Now it tells me: `No changes detected`. I've had this problem before, so what I typically do is `manage.py makemigrations `. Isn't it weird that it only works when I specify the app_name? – Vinu Sep 12 '15 at 05:54
  • `makemigrations` creates migrations, and `migrate` applies those migrations. So when you create an empty database, and delete all migration files, you should first run `makemigrations` to create the migration files, and then apply those with `migrate`. Running `migrate` on an empty database without any migration files will do nothing apart from setup django (and other apps, if used) base tables. Your models will not be translated into tables without migration files. – coolharsh55 Sep 12 '15 at 07:09
  • look at his answer for why you need to create migrations with app name instead of the generic `make migrations` statement: http://stackoverflow.com/questions/24912173/django-1-7-makemigrations-not-detecting-changes – coolharsh55 Sep 12 '15 at 07:10