Opened 9 years ago
Closed 9 years ago
#26521 closed Cleanup/optimization (fixed)
CreateModel allows duplicate field names
Reported by: | w0rp | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
I hit this bug while manually editing a migration file after squashing files. If you define the same field twice in a list of fields for CreateModel, then migrate will take the second field definition without reporting any errors.
class Migration(migrations.Migration): operations = [ migrations.CreateModel( name='Foo', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('created', models.DateTimeField(auto_now_add=True, db_index=True)), # This one will be used. ('created', models.DateField(auto_now_add=True, db_index=True)), ], ), ]
I think the migration code should check for duplicate fields, and then report an error if you have mistakenly defined the same field twice for the fields array.
Change History (10)
comment:1 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Uncategorized → Cleanup/optimization |
comment:3 by , 9 years ago
I don't know if we really need this check, but if we do that, the manager names need to be unique and so do the base classes.
comment:4 by , 9 years ago
Patch needs improvement: | set |
---|---|
Version: | 1.9 → master |
comment:7 by , 9 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
I think this broke non-model base classes:
File "/home/collin/comcenter/../comcenter/category/migrations/0001_initial.py", line 9, in <module> class Migration(migrations.Migration): File "/home/collin/comcenter/../comcenter/category/migrations/0001_initial.py", line 35, in Migration bases=(models.Model, comcenter.product.models.WhiteBlackListMixin), File "/home/collin/comcenter/django/db/migrations/operations/models.py", line 62, in __init__ for base in self.bases File "/home/collin/comcenter/django/db/migrations/operations/models.py", line 17, in _check_for_duplicates for val in objs: File "/home/collin/comcenter/django/db/migrations/operations/models.py", line 63, in <genexpr> if base is not models.Model) AttributeError: type object 'WhiteBlackListMixin' has no attribute 'lower'
comment:8 by , 9 years ago
Will take care of this. I'm a bit surprised CreateModel
is untested with mixins.
comment:10 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Probably wouldn't hurt.