Skip to content
Advertisement

Django Migrations Circular Dependencies

so my django project was working completely fine and everything worked.

I wanted to rename an app something else so I did and updated all the associated files in including the app.py config file.

I also cleared the database and removed all migration files from each app.

Ever since then I have never been able to finish makemigrations onto my apps.

I even recreated the app I renamed, by doing django-admin startapp “appname” and then copied the contents of models.py admin.py, etc over to see if I somehow cause an internal issue but I just can’t figure out what’s going on?.

I did manage to get all the makemigrations to success for all apps including the one I remade when I removed this (below) from another apps admin.py file

# accounts/admin.py

class SigBotSettingsInLine(admin.StackedInline):
    model = SigBotSettings

@admin.register(Bot)
class BotAdmin(admin.ModelAdmin,):
    ...
    inlines = [SigBotSettingsInLine]

but in the end the python manage.py migrate, still Failed. If someone would help it would be highly appreciated.

Here is the error-code:

(dguacENV) PS C:UsersAdminDesktopdjangoProjectsdguac> python manage.py makemigrations accounts
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangocoremanagement__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangocoremanagementbase.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangocoremanagementbase.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangocoremanagementbase.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangocoremanagementcommandsmakemigrations.py", line 88, in handle
    loader = MigrationLoader(None, ignore_no_migrations=True)
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangodbmigrationsloader.py", line 53, in __init__
    self.build_graph()
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangodbmigrationsloader.py", line 286, in build_graph
    self.graph.ensure_not_cyclic()
  File "C:UsersAdminanaconda3envsdguacENVlibsite-packagesdjangodbmigrationsgraph.py", line 274, in ensure_not_cyclic
    raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: accounts.0001_initial, accounts.0002_auto_20210809_1814, signals.0001_initial

Dependencies in accounts/migrations/0001_initial.py

dependencies = [
    ('signals', '__first__'),
    ('auth', '0012_alter_user_first_name_max_length'),
]

Dependencies in accounts/migraitons/0002_auto_20210809_1910.py

dependencies = [
    ('accounts', '0001_initial'),
]

Dependencies in signals/migrations/0001_initial.py

dependencies = [
    ('accounts', '0002_auto_20210809_1910'),
]

Advertisement

Answer

Dependencies in accounts/migrations/0001_initial.py

dependencies = [
    ('signals', '__first__'),
    ('auth', '0012_alter_user_first_name_max_length'),
]

Dependencies in accounts/migraitons/0002_auto_20210809_1910.py

dependencies = [
    ('accounts', '0001_initial'),
]

Dependencies in signals/migrations/0001_initial.py

dependencies = [
    ('accounts', '0002_auto_20210809_1910'),
]

This tells us the following:

  1. accounts.0001.initial depends on signals.0001_initial (specified by '__first__'.
  2. accounts.0002_auto_20210809_1910 depends on accounts.0001.initial
  3. signals.0001_initial.py depends on accounts.0002_auto_20210809_1910

The circularity should be clear.

You can either edit these dependencies manually or delete all migrations and generate them again with ./manage.py makemigrations This should solve the problem unless there are underlying issues with the model dependencies themselves.

WARNING

Be very careful when editing or deleting migrations. If your project is deployed to a live server, editing migrations that have already applied will at best do nothing and at worst completely wreck your production database. The proceeding suggestion should only be used if you are working on a project that has not yet been deployed.

Advertisement