Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

I use Django1.7 with Mezzanine. I create simple profile (according to Mezzanine documentation) stored in separate app "profiles":

class RoadmapProfile(models.Model):
    user = models.OneToOneField("auth.User")
    fullname = models.CharField(max_length=100, verbose_name="Full name")

Creation of migrations returns:

  Migrations for 'profiles':
      0001_initial.py:
        - Create model RoadmapProfile

When I run "migrate profiles":

Operations to perform:
  Apply all migrations: profiles
Running migrations:
  No migrations to apply.

The issue is, when I try to open any page related to mezzanine.accounts (for example update account), it crashes with:

OperationalError at /accounts/update/

no such column: profiles_roadmapprofile.fullname

What I have done wrong?

like image 370
matousc Avatar asked Sep 21 '14 11:09

matousc


2 Answers

  1. In MySQL Database delete row 'profiles' from the table 'django_migrations'.
  2. Delete all migration files in migrations folder.
  3. Try again python manage.py makemigrations and python manage.py migrate command.
like image 192
Zulfugar Ismayilzadeh Avatar answered Nov 25 '22 10:11

Zulfugar Ismayilzadeh


I am a Django newbie and I was going through the same problem. These answers didn't work for me. I wanted to share how did I fix the problem, probably it would save someone lots of time.

Situation:

I make changes to a model and I want to apply these changes to the DB.

What I did:

Run on shell:

python manage.py makemigrations app-name
python manage.py migrate app-name

What happened:

  • No changes are made in the DB

  • But when I check the db schema, it remains to be the old one

Reason:

  • When I run python manage.py migrate app-name, Django checks in django_migrations table in the db to see which migrations have been already applied and will skip those migrations.

What I tried:

Delete the record with app="my-app-name" from that table (delete from django_migrations where app = "app-name"). Clear my migration folder and run python manage.py makemigration my-app-name, then python manage.py migrate my-app-name. This was suggested by the most voted answer. But that doesn't work either.

Why?

Because there was an existing table, and what I am creating was a "initial migration", so Django decides that the initial migration has already been applied (Because it sees that the table already exists). The problem is that the existing table has a different schema.

Solution 1:

Drop the existing table (with the old schema), make initial migrations, and applied again. This will work (it worked for me) since we have an "initial migration" and there was no table with the same name in our db. (Tip: I used python manage.py migrate my-app-name zero to quickly drop the tables in the db)

Problem? You might want to keep the data in the existing table. You don't want to drop them and lose all of the data.

Solution 2:

  1. Delete all the migrations in your app and in django_migrations all the fields with django_migrations.app = your-app-name How to do this depends on which DB you are using Example for MySQL: delete from django_migrations where app = "your-app-name";

  2. Create an initial migration with the same schema as the existing table, with these steps:

    • Modify your models.py to match with the current table in your database

    • Delete all files in "migrations"

    • Run python manage.py makemigrations your-app-name

    • If you already have an existing database then run python manage.py migrate --fake-initial and then follow the step below.

  3. Modify your models.py to match the new schema (e.i. the schema that you need now)

  4. Make new migration by running python manage.py makemigrations your-app-name

  5. Run python manage.py migrate your-app-name

This works for me. And I managed to keep the existing data.

More thoughts:

The reason I went through all of those troubles was that I deleted the files in some-app/migrations/ (the migrations files). And hence, those migration files and my database aren't consistent with one another. So I would try not modifying those migration files unless I really know what I am doing.

like image 27
phanhuy152 Avatar answered Nov 25 '22 09:11

phanhuy152