Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31742 closed Bug (fixed)

makemigrations crashes for ForeignKey with mixed-case app name.

Reported by: Ignacio Santolin Owned by: Mariusz Felisiak
Component: Migrations Version: 3.1
Severity: Release blocker Keywords:
Cc: Adam Johnson Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When i run "python3 manage.py migrate" on Django 3.1b1 shows me that error (Please, note that the code works well in 3.0)

ValueError: The field DJ_RegLogin.Content.category was declared with a lazy reference to 'dj_reglogin.category', but app 'dj_reglogin' isn't installed.

model.py (Conflict Part)

class Category(models.Model):
    title = models.CharField(max_length=100, db_index=True)
    slug = models.SlugField(max_length=100, db_index=True)

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('view_blog_category', None, kwargs={'slug': self.slug})


class Content(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = RichTextField(config_name='default')
    posted = models.DateTimeField(db_index=True, auto_now_add=True)
    sites = models.ManyToManyField(Site)
    ip = models.GenericIPAddressField(editable=False)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False, editable=False)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('view_blog_post', None, kwargs={'slug': self.slug})

settings.py (Related to issue part)

INSTALLED_APPS = [
    'DJ_RegLogin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social_django',
    'ckeditor',
    'django.contrib.sites',
    'django.contrib.flatpages',
    'django.contrib.sitemaps',
]

apps.py

from django.apps import AppConfig


class DJ_RegLoginConfig(AppConfig):
    name = 'DJ_RegLogin'
    verbose_name = "Contents"

Change History (11)

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Adam Johnson added
Summary: Not working model on 3.1 (lazy reference)makemigrations crashes for ForeignKey with mixed-case app name.
Triage Stage: UnreviewedAccepted

Thanks for the report.

Regression in 9e1b6b8a66af4c2197e5b1b41eb9dbb36e4f6502.
Reproduced at fbe82f82555bc25dccb476c749ca062f0b522be3.

comment:2 by Mariusz Felisiak, 4 years ago

Potential fix:

diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index d517d7269b..894d931d50 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -582,7 +582,8 @@ class ForeignObject(RelatedField):
         if self.remote_field.parent_link:
             kwargs['parent_link'] = self.remote_field.parent_link
         if isinstance(self.remote_field.model, str):
-            kwargs['to'] = self.remote_field.model.lower()
+            app_label, model_name = self.remote_field.model.split('.')
+            kwargs['to'] = '%s.%s' % (app_label, model_name.lower())
         else:
             kwargs['to'] = self.remote_field.model._meta.label_lower
         # If swappable is True, then see if we're actually pointing to the target

comment:3 by Adam Johnson, 4 years ago

That fix looks good to me.

comment:4 by Mariusz Felisiak, 4 years ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:5 by Simon Charette, 4 years ago

Wonder if a longer term solution is to use case insensitive identifier for app labels as well.

in reply to:  5 comment:6 by Mariusz Felisiak, 4 years ago

Replying to Simon Charette:

Wonder if a longer term solution is to use case insensitive identifier for app labels as well.

Maybe, I'm afraid that it will create some regressions, since app labels were always 🤔 case sensitive in migrations . This small patch should be fine for backport.

comment:7 by Simon Charette, 4 years ago

Agreed, safer to stick to the less intrusive backport for the time being.

comment:8 by Mariusz Felisiak, 4 years ago

Has patch: set

comment:9 by Adam Johnson, 4 years ago

Case insensitive app labels would make sense. PEP8 recommends lowercase only module names, and using mixing case in filenames can be fraught with errors between different filesystems where case sensitivity in comparisons and naming varies widely.

comment:10 by GitHub <noreply@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 62d85a2:

Fixed #31742 -- Fixed makemigrations crash on ForeignKey to an app with mixed case label.

Regression in 9e1b6b8a66af4c2197e5b1b41eb9dbb36e4f6502.

Thanks Ignacio Santolin for the report.

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 5263480d:

[3.1.x] Fixed #31742 -- Fixed makemigrations crash on ForeignKey to an app with mixed case label.

Regression in 9e1b6b8a66af4c2197e5b1b41eb9dbb36e4f6502.

Thanks Ignacio Santolin for the report.
Backport of 62d85a283500e9abb0e1c9ec53c59be468f056a0 from master

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