Opened 6 years ago

Closed 2 years ago

#14028 closed Bug (fixed)

setting db_column to same value as another column should not validate

Reported by: akaariai Owned by: helenst
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 5 years ago by tttallis

  • Keywords sprintdec2010 added
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to tttallis
  • Patch needs improvement unset
  • Summary changed from setting db_column to same value than another column should not validate to setting db_column to same value as another column should not validate
  • Triage Stage changed from Unreviewed to Accepted

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

comment:2 Changed 5 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:3 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:4 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:5 Changed 2 years ago by timo

  • 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 2 years ago by helenst

  • Owner changed from tttallis to helenst
  • Status changed from new to assigned

Working on this at the PyConUK Django sprint

comment:7 Changed 2 years ago by helenst

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 2 years ago by helenst (previous) (diff)

comment:8 Changed 2 years ago by helenst

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

comment:9 Changed 2 years ago by helenst

I have created a pull request which fixes this:

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

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

  • Resolution set to fixed
  • Status changed from assigned to closed

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