Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.migrations.exceptions.CircularDependencyError

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.

like image 289
yarsanich Avatar asked Nov 20 '16 14:11

yarsanich


People also ask

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

What does Makemigrations do in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

How does Django know which migrations have run?

Django writes a record into the table django_migrations consisting of some information like the app the migration belongs to, the name of the migration, and the date it was applied.


1 Answers

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.

like image 167
Alasdair Avatar answered Oct 03 '22 22:10

Alasdair