Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33515 closed Bug (fixed)

ManyToManyField to lowercased swappable setting causes generating infinite migrations.

Reported by: Chris Lee Owned by: Mariusz Felisiak
Component: Migrations Version: 4.0
Severity: Release blocker Keywords: M2M migration user manytomany
Cc: Simon Charette, David Wobrock Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I create a custom user model that extends AbstractUser and then try to add a ManyToManyField that references this custom User model, django keeps making the same AlterField migration over and over again unnecessarily. I've attached a Git repository that I used to reproduce this issue with very simple code. You can see the two erroneous migrations after the initial migration. If I use the built in user, there is no issue. It seems to appear once I extend AbstractUser.

Git repository: https://github.com/SentientClee/django-bug-reproduction

Here is the accounts app model.py code.

from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    pass


class Test(models.Model):
    members = models.ManyToManyField(settings.AUTH_USER_MODEL)

Here is one of the erroneous migrations. Notice it depends on 0002_alter_test_members which is an erroneous migrations file that looks just like this one.

# Generated by Django 4.0.2 on 2022-02-15 01:33

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterField(
            model_name='test',
            name='members',
            field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
        ),
    ]

Change History (6)

comment:1 by Chris Lee, 2 years ago

Summary: Adding ManyToMany field using setttings.AUTH_USER_MODEL creates unnecessary migrationsAdding ManyToMany field using custom User model (AbstractUser) creates unnecessary migrations

comment:2 by Mariusz Felisiak, 2 years ago

Cc: Simon Charette David Wobrock added
Severity: NormalRelease blocker
Summary: Adding ManyToMany field using custom User model (AbstractUser) creates unnecessary migrationsManyToManyField to lowercased swappable setting causes generating infinite migrations.
Triage Stage: UnreviewedAccepted

Thanks for the report!

Lowercased AUTH_USER_MODEL = 'accounts.user' is crucial to reproduce this issue.

Regression in 43289707809c814a70f0db38ca4f82f35f43dbfd.

comment:3 by Mariusz Felisiak, 2 years ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:4 by Mariusz Felisiak, 2 years ago

Has patch: set

comment:5 by GitHub <noreply@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 1e2e1be:

Fixed #33515 -- Prevented recreation of migration for ManyToManyField to lowercased swappable setting.

Thanks Chris Lee for the report.

Regression in 43289707809c814a70f0db38ca4f82f35f43dbfd.

Refs #23916.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

In 760b7e7f:

[4.0.x] Fixed #33515 -- Prevented recreation of migration for ManyToManyField to lowercased swappable setting.

Thanks Chris Lee for the report.

Regression in 43289707809c814a70f0db38ca4f82f35f43dbfd.

Refs #23916.
Backport of 1e2e1be02bdf0fe4add0d0279dbca1d74ae28ad7 from main

Note: See TracTickets for help on using tickets.
Back to Top