Django

Code

Ticket #5671 (closed: fixed)

Opened 1 year ago

Last modified 5 months ago

Change SQL generation when primary_key=True and unique=True

Reported by: Nis Jørgensen <nis@superlativ.dk> Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords: db-be-api
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

At the moment, these two models, which are logically equivalent, generate different sql:

class Country(models.Model):
    isocode = CharField(max_length=3, primary_key=True, unique=True)

class Country(models.Model):
    isocode = CharField(max_length=3, primary_key=True)

- at least for backends other than Oracle

I would like to suggest that we do not create explicit UNIQUE constraints for (single field) primary keys.

On the other hand, I think it would be nice if field.unique returned True for a primary key field no matter if it was defined as such or not.

I will write a patch doing this, but am of course open to alternative suggestions.

Attachments

unique.diff (4.8 kB) - added by Nis Jørgensen <nis@superlativ.dk> on 10/04/07 03:34:48.
Patch doing both of the things mentioned in the article
unique.2.diff (4.8 kB) - added by Nis Jørgensen <nis@superlativ.dk> on 10/04/07 03:35:06.
Patch doing both of the things mentioned in the ticket

Change History

10/04/07 03:34:48 changed by Nis Jørgensen <nis@superlativ.dk>

  • attachment unique.diff added.

Patch doing both of the things mentioned in the article

10/04/07 03:35:06 changed by Nis Jørgensen <nis@superlativ.dk>

  • attachment unique.2.diff added.

Patch doing both of the things mentioned in the ticket

10/04/07 03:42:46 changed by Nis Jørgensen <nis@superlativ.dk>

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests changed.
  • needs_docs changed.

Oops - double submit. The patches are identical.

The patch does the following:

If a field is declared as primary_key, it is automatically flagged as unique

If a field is declared as primary_key, no UNIQUE is output in the SQL.

All checks of the type

if f.primary_key or f.unique:

are replaced by

if f.unique:

12/01/07 20:29:41 changed by Simon G <dev@simon.net.nz>

  • stage changed from Unreviewed to Design decision needed.

12/08/07 06:45:42 changed by mtredinnick

  • stage changed from Design decision needed to Accepted.

The concept seems reasonable. The patch introduces some random formatting changes that are not part of the change needed, but otherwise looks reasonable from just eyeballing it. I haven't tested it on all (or, in fact, any) db backends, so I'm not going to move it to almost ready-for-checkin just yet.

06/25/08 11:36:56 changed by ramiro

  • keywords set to db-be-api.

06/29/08 23:47:00 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [7790]) Make sure we only create the minimum number of table indexes for MySQL.

This patch simplifies a bunch of code for all backends and removes some duplicate index creation for MySQL, in particular (versions 4.x and later). Patch from Nis Jørgensen.

Fixed #5671, #5680, #7170, #7186.


Add/Change #5671 (Change SQL generation when primary_key=True and unique=True)




Change Properties
Action