Django

Code

Ticket #2257 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] syncdb should generate unique constraint names with hashes

Reported by: mir@noris.de Assigned to: mtredinnick
Milestone: Component: django-admin.py
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Take a look at how syncdb names constraints:

backend.quote_name('%s_referencing_%s_%s' % (r_col, table, col))

In some circumstances, syncdb generates the same constraint name (for foreign keys= for different tables. Now, using both table names in the name can lead to too long names, so this isn't a solution. I propose to use a hash of both table names + the two attribute names. A quick, easy and dirty version is:

backend.quote_name('%s_referencing_%s_%x' % (r_col, col, hash((r_table, table))))

Here's an example.

class Man(models.Model):
  father = ForeignKey(Man, null=True)

class Woman(models.Model):
  father = ForeignKey(Man, null=True)

These will (currently) both create the constraint 'father_referencing_man_id' (resulting in incomprehensible error message in case of mysql)

Attachments

2257_fullname_1.patch (1.2 kB) - added by bill@dehora.net on 07/23/06 09:12:40.
supplies db wide unique fk names (for mysql)

Change History

07/18/06 21:55:43 changed by mtredinnick

  • owner changed from adrian to mtredinnick.

07/19/06 05:15:53 changed by mtredinnick

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

The auto-closer doesn't seem to want to pick this up (in [3373]):

Fixed #2257 -- MySQL wants constraint names to be unique per-database, so fixed the SQL generation to ensure this.

07/19/06 20:29:56 changed by mtredinnick

  • status changed from closed to reopened.
  • resolution deleted.

David Avraamides pointed out (politely) that I suck. The solution in [3373] only works when all references are generated at the same time. I'll write a better fix shortly; reopening for now, since people seem to be hitting this about once a day.

07/23/06 09:12:40 changed by bill@dehora.net

  • attachment 2257_fullname_1.patch added.

supplies db wide unique fk names (for mysql)

07/23/06 09:13:13 changed by bill@dehora.net

  • summary changed from syncdb should generate unique constraint names with hashes to [patch] syncdb should generate unique constraint names with hashes.

patch produces unique fk names. This one uses the full table names in the fk name (I have another that uses the col names and hashes the two tablenames at the end if you want it). The memo dict reference_names has been taken out.

08/01/06 16:46:51 changed by mtredinnick

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

(In [3512]) Fixed #2257 -- make constraint names unique across all table/column combinations (for the benefit of MySQL).

08/01/06 16:48:25 changed by mtredinnick

Thanks for the patch, Bill, but I ended up going with a hash of the table names at the end, just to save a few characters.

For people reading this in the future, the reason I am not just punting this and using no name so that MySQL can make on up is because it makes debugging a little easier to see a constraint name that makes sense when you violate it. It's worth a little pain to try and get that right.


Add/Change #2257 ([patch] syncdb should generate unique constraint names with hashes)




Change Properties
Action