15

I have a problem with Django migrations on empty DB. When I want to migrate I have a circular dependency error. Circular dependency error between two apps that related by foreign keys

/firstapp/models.py

class Person(models.Model):
   ...


class Doctor(Person):
    hospital = models.ForeignKey('hospital.Hospital', on_delete=models.SET_NULL, null=True, default=None,blank = True)
    ...

class Patient(Person):
    doctor = models.ForeignKey('Doctor', on_delete=models.SET_NULL, null=True, default=None)

/secondapp/models.py

class Hospital(models.Model):
    ...
    main_doctor = models.ForeignKey('authoriz.Doctor', on_delete=models.SET_NULL, null=True,verbose_name="Main Doctor")
    calendar = models.ForeignKey('schedule.Calendar',verbose_name="calendar",null = True)
    ...

class Seat(models.Model):
    hospital = models.ForeignKey('Hospital', on_delete=models.CASCADE)
    ...

After

python manage.py migrate

Traceback

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/user/project/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/project/lib/python3.5/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/user/project/lib/python3.5/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/home/user/project/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 136, in handle
    plan = executor.migration_plan(targets)
  File "/home/user/project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 60, in migration_plan
    for migration in self.loader.graph.forwards_plan(target):
  File "/home/user/project/lib/python3.5/site-packages/django/db/migrations/graph.py", line 280, in forwards_plan
    self.ensure_not_cyclic(target, lambda x: (parent.key for parent in self.node_map[x].parents))
  File "/home/user/project/lib/python3.5/site-packages/django/db/migrations/graph.py", line 370, in ensure_not_cyclic
    raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: authoriz.0001_initial, hospital.0001_initial

Thanks for help.

yarsanich
  • 174
  • 1
  • 1
  • 10

4 Answers4

28

Temporarily comment out foreign keys to break the circular dependency. It looks like you could do this by commenting out Hospital.doctor. Remove the existing migrations and run makemigrations to recreate them.

Finally, uncomment the foreign keys, and run makemigrations again. You should end up with migrations without any circular dependencies.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I have the same issue but, in my case, I don't get the name of the models in circular dependency, I just get the names of the apps. How can I know which models are circularly dependent? – HuLu ViCa Oct 01 '18 at 02:55
  • @HugoLuisVillalobosCanto the error message here didn't name the models either, I had to look at the foreign keys between apps. We can't help you in the comments here, because we don't know your models. – Alasdair Oct 01 '18 at 08:22
  • There must be a better way, right? Why is there not another solution other than commenting and uncommenting. –  Dec 14 '20 at 06:14
  • @NickSmith this question is several years old, uses made-up variables like `firstapp`, and doesn't include the migrations that were failing, so it's not the best place to try to solve your problem. When I created two apps with fks to each other in Django 3.1, it created two migrations for one of the apps and avoided a circular dependency in the migrations, so no commenting-out was necessary. – Alasdair Dec 14 '20 at 10:05
  • @Alasdair Cool to see a response!! I am still getting the err, can I post a new question with my details and paste the link here? –  Dec 14 '20 at 19:39
  • @NickSmith No promises I'll be able to help, but if you post a new question I'll have a look. – Alasdair Dec 15 '20 at 10:33
  • @Alasdair Would love your help... question is posted here: https://stackoverflow.com/questions/65332466/circulardependencyerror-on-foreign-keys-pointing-to-each-other-django THANK YOU SOOOO MUCH –  Dec 16 '20 at 23:12
0

Should be useful if you add your Calendar model. However don't use the inheritance without abstract modal.

class Person(models.Model):
    ...

    class Meta:
        abstract = True
0

Like you might have defined them as something below:

new_field = models.ForeignKey(ForeignModel, ...)

Instead do this:

new_field = models.ForeignKey("ForeignModel", ...)
pmadhu
  • 3,373
  • 2
  • 11
  • 23
0
  1. Delete your existing virtual environment and create a new one.
  2. In your models files, comment out all ForeignKey fields.
  3. Run migrations and migrate
  4. Uncomment the ForeignKey fields you commented
  5. Again run migrations and migrate
Asif
  • 56
  • 6