Opened 22 months ago

Closed 22 months ago

Last modified 22 months ago

#34351 closed Cleanup/optimization (duplicate)

Slash in constraint name results in invalid migration file naming

Reported by: Simon Dupouy Owned by: nobody
Component: Migrations Version: 4.1
Severity: Normal Keywords: constraint name slash
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Steps to reproduce:

  1. create a model
    class TestModel(models.Model):
        foo = models.TextField()
        bar = models.TextField()
    
  2. make and apply the initial migration
  3. add a constraint with a forward slash in its name
    class TestModel(models.Model):
        foo = models.TextField()
        bar = models.TextField()
    
        class Meta:
            constraints = [
                models.UniqueConstraint(fields=['foo', 'bar'], name='foo/bar unique')
            ]
    
  4. makemigrations and migrate

Result

Django tries to create a migration file named 0002_testmodel_foo/bar unique.py.
This results in the creation of the file bar unique.py in the directory 0002_testmodel_foo.
Hence, the last migrate command doesn't work.

Expected behavior

Creation of a migration file with a sanitized name, or a warning that the constraint name shouldn't contain special characters.

Remark

All that the documentation says on constraint names is

BaseConstraint.name
The name of the constraint. You must always specify a unique name for the constraint.

It isn't stated that the naming of constraints must follow specific conventions.

Change History (4)

comment:1 by Mariusz Felisiak, 22 months ago

Easy pickings: unset
Resolution: duplicate
Status: newclosed
Type: BugCleanup/optimization

Duplicate of #30614.

Different databases have different policies for constraint and index names. I don't think it's worth the additional complexity as databases throw a clear error for invalid names.

comment:2 by Dustin Wyatt, 22 months ago

I think that this never gets to the database for the database to give an error...right? If I'm correct, I'm not sure what the appropriate fix is (if any). I'm not sure that "databases give good error messages for this problem" is a good resolution to a problem that databases do not give good error messages for.

in reply to:  2 comment:3 by Simon Dupouy, 22 months ago

Replying to Dustin Wyatt:

I think that this never gets to the database for the database to give an error...right? If I'm correct, I'm not sure what the appropriate fix is (if any). I'm not sure that "databases give good error messages for this problem" is a good resolution to a problem that databases do not give good error messages for.

Indeed you are correct, the problem is in the way the makemigrations command finds a name for the migration file. Hence it is not exactly a duplicate issue in my opinion.

comment:4 by Mariusz Felisiak, 22 months ago

Right, so it's a duplicate of #34050 (fixed in Django 4.2).

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