#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:
- create a model
class TestModel(models.Model): foo = models.TextField() bar = models.TextField()
- make and apply the initial migration
- 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') ]
makemigrations
andmigrate
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 , 21 months ago
Easy pickings: | unset |
---|---|
Resolution: | → duplicate |
Status: | new → closed |
Type: | Bug → Cleanup/optimization |
follow-up: 3 comment:2 by , 21 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.
comment:3 by , 21 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.
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.