Skip to content

Custom AUTH_USER_MODEL Creates Migration Circular Reference? #215

@vector-kerr

Description

@vector-kerr

Greetings!

I'm having a go at using django-organizations, and I've been getting a circular reference error when attempting to perform migrations, which I believe I can trace back to django-organizations and using a custom AUTH_USER_MODEL.

I say 'I believe' because I'm not 100% sure that I haven't done something incorrectly, but having read the documentation on using a custom user model and proxy models, I think I've covered myself...

I've included a set of steps to reproduce below which should hopefully demonstrate that either I'm doing something woefully wrong, or there's something weird going on with custom user models.

If you need any more information, or if I've stupidly broken it somehow, please let me know. :)

Thanks for your help and your time!
- Vector

Steps to reproduce

  1. Create and activate a virtual environment:
    python3 -m venv .venv
    source .venv/bin/activate
  2. Install the following python requirements:
    appdirs==1.4.4
    asgiref==3.3.4
    distlib==0.3.1
    Django==3.2
    django-extensions==3.1.3
    django-organizations==2.0.0
    filelock==3.0.12
    pytz==2021.1
    six==1.15.0
    sqlparse==0.4.1
    virtualenv==20.4.2
    
  3. Create a new django project (I've called mine demo) and enter the directory:
    django-admin startproject demo
    cd demo
  4. Create an app called core which will be used for models and migrations
    python3 manage.py startapp core
  5. Put the following content into core/models.py:
    from django.db import models
    from django.contrib.auth.models import AbstractUser
    from organizations.models import Organization
    from organizations.models import OrganizationUser
    
    class User(AbstractUser):
        created_at = models.DateTimeField(auto_now_add=True, editable=False)
        updated_at = models.DateTimeField(auto_now_add=True)
        def __str__(self):
            return self.username
    
    class Tenant(Organization):
        class Meta:
            proxy = True
    
    class TenantUser(OrganizationUser):
        class Meta:
            proxy = True
  6. Update demo/settings.py to include the organizations and core apps, and set the custom user model:
    INSTALLED_APPS = [
        # ...
        'organizations',
        'core',
    ]
    
    AUTH_USER_MODEL = 'core.User'
  7. Create the migrations for the project:
    python3 manage.py makemigrations
    At this point, I get the first bit of weird output - a migration is created against the organizations module in the virtual environment:
    Migrations for 'organizations':
      .venv/lib/python3.9/site-packages/organizations/migrations/0005_auto_20210430_0635.py
        - Alter field id on organization
        - Alter field id on organizationinvitation
        - Alter field id on organizationowner
        - Alter field id on organizationuser
    Migrations for 'core':
      core/migrations/0001_initial.py
        - Create proxy model Tenant
        - Create proxy model TenantUser
        - Create model User
    
  8. Finally, attempt to create the migrations again. This time, I receive a circular reference error, which links back to the previously created migration:
    python3 manage.py makemigrations
    Traceback (most recent call last):
    [...snip...]
    django.db.migrations.exceptions.CircularDependencyError: core.0001_initial, organizations.0001_initial, organizations.0002_model_update, organizations.0003_field_fix_and_editable, organizations.0004_organizationinvitation, organizations.0005_auto_20210430_0635
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions