Opened 10 months ago

Closed 9 months ago

#23028 closed New feature (fixed)

Add unique_together support to inspectdb

Reported by: anonymous Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords:
Cc: damien.nozay@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

django/db/backends/mysql/introspection.py has a get_constraints() method.

This is what get_indexes() would return

{u'commit': {'primary_key': False, 'unique': False},
 u'repository': {'primary_key': False, 'unique': False}}

This is what get_constraints() would return

{u'commit_index': {'check': False,
                   'columns': [u'commit'],
                   'foreign_key': None,
                   'index': True,
                   'primary_key': False,
                   'unique': False},
 u'repo_index': {'check': False,
                 'columns': [u'repository'],
                 'foreign_key': None,
                 'index': True,
                 'primary_key': False,
                 'unique': False},
 u'unique_build': {'check': False,
                   'columns': [u'number', u'jobname', u'repository'],
                   'foreign_key': None,
                   'index': True,
                   'primary_key': False,
                   'unique': True}}

This is what manage.py inspectdb currently returns

class Builds(models.Model):
    number = models.IntegerField(blank=True, null=True)
    jobname = models.CharField(max_length=64)
    repository = models.CharField(max_length=64)
    commit = models.CharField(max_length=40)
    class Meta:
        managed = False
        db_table = 'builds'

it could be smarter and invoke get_constaints() to add the correct unique_together clause in Meta.

unique_together = ((u'number', u'jobname', u'repository'),)

Change History (3)

comment:1 Changed 10 months ago by dnozay

  • Cc damien.nozay@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

reported by dnozay.

patch is at https://github.com/django/django/pull/2920.patch

comment:2 Changed 10 months ago by timo

  • Component changed from Uncategorized to Core (Management commands)
  • Has patch set
  • Patch needs improvement set
  • Summary changed from inspectdb does not check unique_together constraints to Add unique_together support to inspectdb
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to New feature
  • Version changed from 1.6 to master

Added some comments for improvement on the PR. Please uncheck "Patch needs improvement" when you update it, thanks.

comment:3 Changed 9 months ago by Tim Graham <timograham@…>

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

In 70c54a3694975c43a2e0e22c4a90c3cbe1eb54e4:

Fixed #23028: Added unique_togther support to inspectdb.

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