Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8: Create initial migrations for existing schema

I started a django 1.8 project, which uses the migrations system.
Somehow along the way things got messy, so I erased the migrations folders and table from the DB, and now I'm trying to reconstruct them, with no success.

I have three apps (3 models.py files), and the models reflect the tables EXACTLY!

The best approach that I've found so far was:

  1. Erase all migrations folders. Done!
  2. Delete everything from the django_migrations table. Done!
  3. Run python manage.py makemigrations --empty <app> for every app. Done!
  4. Run python manage.py migrate --fake. Done! (although it works only if I run it after every makemigrations command.

Now I add a new field, run the makemigrations command, and I receive the following error:
django.db.utils.OperationalError: (1054, "Unknown column 'accounts_plan.max_item_size' in 'field list'")

I've been burning HOURS on this thing. How the h**l can I initialize the migrations so I can continue working without migration interruptions every time?

Why is it so complicated? Why isn't there a simple one-liner: initiate_migrations_from_schema?

EDIT:
Now things get even nastier. I truncated the django_migrations table and deleted all the migrations folder.
Now I try to run python manage.py migrate --fake-initial (something I found in the DEV docs), just so it sets up all of Django's 'internal' apps (auth, session, etc) and I'm getting:
(1054, "Unknown column 'name' in 'django_content_type'").
Now, this "column" is not a real column. It's a @property defined in Django's contenttypes app. WHAT IS GOING ON HERE? Why is it identifying the name property as a real column?

like image 784
user1102018 Avatar asked Apr 27 '15 05:04

user1102018


People also ask

How do I create a new migration in Django?

Create or update a model. Run ./manage.py makemigrations <app_name> Run ./manage.py migrate to migrate everything or ./manage.py migrate <app_name> to migrate an individual app. Repeat as necessary.

What is the difference between Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

How do I reset migrations in Django 3?

Reset the Whole Database in Django sqlite3 and then delete all the migrations folders inside all the apps. After deleting the migrations folders, we can remake the migrations and migrate them using two commands; namely, python manage.py makemigrations and python manage.py migrate .

How do I reapply migrations in Django?

Steps to rerun a Django migration:Fake back to the migration immediately before the one you want to rerun. Rerun the target migration. Fake back to the latest migration.


1 Answers

Finally got it to work, although I don't know why and I hope it will work in the future.
After doing numerous trials and going through Django's dev site (link).
Here are the steps (for whoever runs into this problem):

  1. Empty the django_migrations table: delete from django_migrations;
  2. For every app, delete its migrations folder: rm -rf <app>/migrations/
  3. Reset the migrations for the "built-in" apps: python manage.py migrate --fake
  4. For each app run: python manage.py makemigrations <app>. Take care of dependencies (models with ForeignKey's should run after their parent model).
  5. Finally: python manage.py migrate --fake-initial

After that I ran the last command without the --fake-initial flag, just to make sure.

Now everything works and I can use the migrations system normally.

I'm sure I'm not the only one who encounters this issue. It must be documented better and even simplified.

Update for Django 1.9 users:
I had this scenario again with a Django 1.9.4, and step 5 failed.
All I had to do is replace --fake-initial with --fake to make it work.

like image 88
user1102018 Avatar answered Nov 10 '22 15:11

user1102018