Opened 6 years ago

Closed 3 years ago

#14028 closed Bug (fixed)

setting db_column to same value as another column should not validate

Reported by: Anssi Kääriäinen Owned by: Helen Sherwood-Taylor
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: validation, sprintdec2010
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Consider having a model

class FooBar(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField(db_column='foo')
    class Meta:
        db_table = 'foobar'

This does not raise any validation errors, even though the model clearly is not valid.

Change History (10)

comment:1 Changed 6 years ago by Thomas Ashelford

Keywords: sprintdec2010 added
Owner: changed from nobody to Thomas Ashelford
Summary: setting db_column to same value than another column should not validatesetting db_column to same value as another column should not validate
Triage Stage: UnreviewedAccepted

Agreed, let's fix it. I'll work up a patch.

comment:2 Changed 6 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:3 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:4 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:5 Changed 3 years ago by Tim Graham

Easy pickings: set

Seems like this would be a fairly easy addition to django/core/management/validation.py with a test in django/tests/invalid_models/tests.py.

comment:6 Changed 3 years ago by Helen Sherwood-Taylor

Owner: changed from Thomas Ashelford to Helen Sherwood-Taylor
Status: newassigned

Working on this at the PyConUK Django sprint

comment:7 Changed 3 years ago by Helen Sherwood-Taylor

Should it be valid to do something like this?

class FooBar(models.Model):
    foo = models.IntegerField(db_column='bar')
    bar = models.IntegerField(db_column='foo')
    class Meta:
        db_table = 'foobar'

i.e. should setting db_column on the first column 'free up' the name so the second one can use it?

Last edited 3 years ago by Helen Sherwood-Taylor (previous) (diff)

comment:8 Changed 3 years ago by Helen Sherwood-Taylor

Consensus on #django-dev was that this should be a valid model.

comment:9 Changed 3 years ago by Helen Sherwood-Taylor

I have created a pull request which fixes this:

https://github.com/django/django/pull/1664

comment:10 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 41167645b1039067127fa215d4d28296bfa4cfdc:

Fixed #14028 - Added validation for clashing db_columns.

Thanks akaariai for the suggestion.

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