Search code examples
pythondjangomodelmigrationmakemigrations

python django issue with the migrations


Is there any way to deal with the migrations ?

I have been working with the version 1.8 of django, where, after doing any change in the models.py, we need to run the following commands -

python manage.py makemigrations
python manage.py migrate

Many a times, this gives an error. And it so happens that I have to rebuild the project as there is no way out.

I also tried the following way-outs, but none of them worked.

  • deleted the migrations folder
  • undo the changes to model.py
  • deleted the files inside the migrations folder
  • tried with flushing, squashing the migratios

it every times shows the following error with a very long error log of some unknown files.

Post Edit : Here's the whole log

File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/schema.py", line 147, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)

django.db.utils.IntegrityError: NOT NULL constraint failed: zapp_post__new.specs_order_post_id

Here is the link to my project, which currently is showing the error. You can try running the application

What should be done in this case ?


Solution

  • Downloaded your code, deleted db.sqlite3, ran syncdb, everything worked fine. Since you didnt have any sensitive data in your db i think that works for you.

    Here's a little extra info for future:

    When modifying migrations/DB manually or when you run into a problem with migrations you should consider these things:

    • You should NOT delete migrations folder
    • Migrations folder should always contain __init__.py file
    • All applied migrations are stored in django_migrations table, so if you delete all migration files and remake migrations (i.e. creating a new 0001_initial.py), running migrate wont do anything, because django thinks it's already applied
    • Sometimes deleting specific rows in django_migrations table and also modifying your tables structure (according to deleted rows) solves the problem, but you should know what you're doing.

    So, the easiest solution when you run into a problem with migrations is deleteing all files in migrations folder (except __init__.py), deleting all rows in django_migrations table where app=your_app_name, droping all tables of your app, then remaking migrations and applying them.

    But if you have sensitive data and you can't delete db, it gets more complicated