0

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 ?

vhd
  • 2,008
  • 6
  • 20
  • 21
  • You need to provide more data. Give more of the stack please. – Sylvain Biehler Jul 03 '15 at 08:23
  • @SylvainBiehler, added. – vhd Jul 03 '15 at 08:31
  • Is there any existing data in the DB already? Specifically, does `zapp_post__new.specs_order_post_id` already contains any nulls? – J0HN Jul 03 '15 at 09:04
  • @J0HN Yes, there is some existing data in the DB. But I've already removed the specs_order field from the database. – vhd Jul 03 '15 at 09:13
  • The traceback in the question says that `specs_order_post_id` still existed when you run the migration. Most likely that happened because there were already NULLs in the column, and migration didn't provide any hints to how convert them to non-null values. – J0HN Jul 03 '15 at 09:24
  • I'm voting to close this question as off-topic because it is too narrow - likely caused by manual intervention to DB. – J0HN Jul 03 '15 at 09:24

1 Answers1

2

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

saadin
  • 91
  • 1
  • 8
  • 1
    @vhd, kind of an older post but... Another method for fixing migration issues after changing models.py, which I ran into today, consists of clearing the migrations and setting back to start (i.e. Zero) using a command like: **python manage.py migrate --fake APPNAME zero** This is the reference I used to fix my issues with problematic migrations earlier today and in my case, I did not need to drop my tables: https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html – ViaTech Oct 18 '18 at 20:10