Yuri van Geffen
Yuri van Geffen

Reputation: 983

Django duplicate migrations in apps

Django keeps making duplicate migrations in for my application. I've ran makemigrations and migrate before I changed my model. After the changes I ran makemigrations again to make migrations for the updated model, I got the following error:

CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0001_initial, 0001_initial 3 in sessions; 0002_remove_content_type_name 3, 0002_remove_content_type_name, 0001_initial 3 in contenttypes; 0002_alter_permission_name_max_length 3, 0010_alter_group_name_max_length 3, 0007_alter_validators_add_error_messages 3, 0006_require_contenttypes_0002 3, 0005_alter_user_last_login_null 3, 0001_initial 3, 0008_alter_user_username_max_length 3, 0009_alter_user_last_name_max_length 3, 0011_update_proxy_permissions, 0011_update_proxy_permissions 3, 0004_alter_user_username_opts 3, 0003_alter_user_email_max_length 3 in auth; 0003_logentry_add_action_flag_choices 3, 0002_logentry_remove_auto_add 3, 0003_logentry_add_action_flag_choices, 0001_initial 3 in admin).

To fix them run 'python manage.py makemigrations --merge'

Running makemigrations --merge doesn't work: ValueError: Could not find common ancestor of {'0002_remove_content_type_name', '0002_remove_content_type_name 3', '0001_initial 3'}

There are many duplicate migrations in apps that I haven't touched (auth, admin, etc.):

admin
 [ ] 0001_initial 3
 [X] 0001_initial
 [ ] 0002_logentry_remove_auto_add 3
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
 [ ] 0003_logentry_add_action_flag_choices 3
auth
 [ ] 0001_initial 3
 [X] 0001_initial
 [ ] 0002_alter_permission_name_max_length 3
 [X] 0002_alter_permission_name_max_length
 [ ] 0003_alter_user_email_max_length 3
 [X] 0003_alter_user_email_max_length
 [ ] 0004_alter_user_username_opts 3
 [X] 0004_alter_user_username_opts
 [ ] 0005_alter_user_last_login_null 3
 [X] 0005_alter_user_last_login_null
 [ ] 0006_require_contenttypes_0002 3
 [X] 0006_require_contenttypes_0002
 [ ] 0007_alter_validators_add_error_messages 3
 [X] 0007_alter_validators_add_error_messages
 [ ] 0008_alter_user_username_max_length 3
 [X] 0008_alter_user_username_max_length
 [ ] 0009_alter_user_last_name_max_length 3
 [X] 0009_alter_user_last_name_max_length
 [ ] 0010_alter_group_name_max_length 3
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [ ] 0011_update_proxy_permissions 3
contenttypes
 [ ] 0001_initial 3
 [X] 0001_initial
 [X] 0002_remove_content_type_name
 [ ] 0002_remove_content_type_name 3
database
 (no migrations)
sessions
 [X] 0001_initial
 [ ] 0001_initial 3

The only app that, as far as I know, should've been affected is database. Why is Django making these duplicate migrations? Is there any way I can get rid of these? Or apply them so they are not blocking anymore? How I can make sure this doesn't happen again?

Upvotes: 1

Views: 4083

Answers (2)

Yuri van Geffen
Yuri van Geffen

Reputation: 983

It turned out the duplicates weren't created by Django but by some other process. Completely removing my environment and reinstalling all dependencies helped. Removing the migrations also would've worked.

Upvotes: 1

Juanjo
Juanjo

Reputation: 19

Sometimes, if you have the option, the easiest and fastest way is...

  • Delete migration files.
  • Delete entire database.
  • run "python manage.py makemigrations" again
  • run "python manage.py migrate" again

or... if you have a db in production with data and you can't reset:

  • Check last production version
  • Delete migration files (until this version).
  • Delete entire local(sqlite) database.
  • run "python manage.py makemigrations" again
  • run "python manage.py migrate" again

Important... copy /migrations/ folder somewhere to recover files if necessary.

Upvotes: 1

Related Questions