Wagtail CircularDependencyError

750 views
Skip to first unread message

Vincent Muchai

unread,
Jun 14, 2016, 2:47:28 PM6/14/16
to Wagtail support
Hey, I've been following the tutorial on Custom user models with wagtail and i want to implement it in one of my projects. However when i follow the example and attempt it i keep getting a "CircularDependencyError" message. How can i resolve the problem?
The code for the model i'm using is shown below:

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import AbstractUser
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel


@register_snippet
class County(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)
    # file = models.ImageField(upload_to='CountyLogos/%Y/%m/%d/', null=True)

    file = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )
    date = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=True)

    panels = [
        FieldPanel('name'),
        ImageChooserPanel('file'),
        FieldPanel('active'),
    ]

    class Meta:
        verbose_name_plural = 'Counties'

    def __unicode__(self):
        return self.name


class CountyOfficial(AbstractUser):
    county = models.ForeignKey(
        County,
        on_delete=models.SET_NULL,
        null=True)



The error i keep getting when i run the makemigration command is:

File "/home/vincent/.virtualenvs/jrs/local/lib/python2.7/site-packages/django/db/migrations/graph.py", line 241, in ensure_not_cyclic
    raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: wagtailcore.0001_squashed_0016_change_page_url_path_to_text_field, map.0001_initial, wagtailimages.0013_make_rendition_upload_callable, wagtailimages.0012_copy_image_permissions_to_collections, wagtailcore.0026_group_collection_permission, wagtailcore.0025_collection_initial_data, wagtailcore.0024_collection, wagtailcore.0023_alter_page_revision_on_delete_behaviour, wagtailcore.0022_add_site_name, wagtailcore.0021_capitalizeverbose, wagtailcore.0020_add_index_on_page_first_published_at, wagtailcore.0019_verbose_names_cleanup, wagtailcore.0018_pagerevision_submitted_for_moderation_index, wagtailcore.0017_change_edit_page_permission_description

Matthew Westcott

unread,
Jun 14, 2016, 3:18:46 PM6/14/16
to wag...@googlegroups.com
Hi Vincent,
The problem here is that there's no possible order that Django can create the tables in: your user model depends on the County model, County depends on wagtailimages.Image, and wagtailimages.Image depends on the user model (because it records the user that uploaded the image). To work around this, I'd suggest building up your models over several migrations, rather than doing everything in one go. If you start with a models.py containing just:

from django.contrib.auth.models import AbstractUser

class CountyOfficial(AbstractUser):
pass

then add the County model, then add the 'county' foreign key on CountyOfficial - running `./manage.py makemigrations` after each step - then Django will be able to create the user model as the very first migration, without running into a circular dependency.

Cheers,
- Matt

Vincent Muchai

unread,
Jun 15, 2016, 1:19:57 AM6/15/16
to Wagtail support
Thanks, i used several migrations as you said and now it's working.

R G

unread,
Feb 14, 2020, 4:14:02 AM2/14/20
to Wagtail support
It is painful to run several migrations (I have views, serializers, urls, etc. all using the model objects).

What I found worked (as mentioned but not fully explained in the docs) is to separate my user-type models by creating a new app called "users" and putting my Custom User / AbstractUser class in there.

In your other app, you then import User from users.models.

In your project/settings/base.txt, register your new users app, then add the line AUTH_USER_MODEL = 'users.User'


Worth noting - since I made these changes AFTER already making and applying migrations (resulting in the cyclic dependency issues), I then had errors relating to order of migrations being applied.

I'm still prototyping using SQLite, so I simply deleted the SQLite file, the migrations files from "users app" and migrations from "other app", then re-ran python manage.py makemigrations and migrate.

If you're using postgres or another database, you may not be able to just delete your migrations.
Reply all
Reply to author
Forward
0 new messages