vannorman
vannorman

Reputation: 164

Django migrate broken, won't forget a deleted column

Using django 1.7.5, I made changes to User_Profile by adding a field:

school = models.CharField(max_length = 255, blank=True, default="")

Then,

$ python manage.py makemigrations 

No problem. It added the field. Next,

$ python manage.py migrate

gives me:

django.db.utils.IntegrityError: mathbreakers_userprofile__new.school may not be NULL

Uh oh. Well, after trying different things by making the field default="" or blank=True, nothing works. Forget it, I deleted the field and reverted the model to the way it was when everything worked fine. Model looks like it did before, and I run makemigrations, it removes the field (it says). Great!

However,

$ python manage.py migrate

still results in the SAME error, complaining about the field I added and then deleted. It's as if I can't make django forget that that field exists. But it doesn't exist in my models.py file. How do I fix this? Where is that field? Is it in my SqlLite that I should go in and delete it manually from there?

NOTE: I have manually deleted broken migration files and also reverted to a previous GIT version before the changes. Nothing has worked, python manage.py migrate seems to still be broken..

Upvotes: 3

Views: 7180

Answers (3)

Dhia Shalabi
Dhia Shalabi

Reputation: 1540

You can solve this issue by following these simple steps:

  1. Delete all python generated migrations in migrations folder.
  2. clear all data in django_migrations table manually.

And everything should work.

Upvotes: 0

user3363213
user3363213

Reputation: 1

TL;DR: try editing the latest migrations .py file in app/migrations.

I just had a similar problem myself (Django 1.8.6, Postgresql). I wanted to create a subclass of an existing class, and wanted to move a field from the base class to the subclass. I had (I thought) deleted the offending field from the parent class, but when I ran python manage.py migrate, I was getting was getting an error message saying the field in the subclass was clashing a field of similar name in the base class.

I looked at the last migrations .py file (in app/migrations), and noticed that the command to remove the field from the base class were after the command to create the subclass. So I edited the migrations file (changed the order of commands), and python manage.py migrate ran fine.

Upvotes: 0

vannorman
vannorman

Reputation: 164

Although I am not using South (I think), the following solved my issue:

Backwards migration with Django South

Specifically, I followed these steps:

  • Remove the culprit field from models.py
  • Delete the culprit migration files AND their .pyc counterparts
  • Rolled back migration to the number just before the error, e.g.

$ python manage.py migrate MY_APP_NAME 0024

Running migrations:

No migrations to apply.

Although the last step seemed to do nothing, now when I run makemigrations and migrate, everything is fine with zero errors.

Upvotes: 4

Related Questions