Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9253 -- Modified the method used to generate constraint names …

…so that it is consistent regardless of machine word size.

NOTE: This change is backwards incompatible for some users.

If you are using a 32-bit platform, you will observe no differences as a
result of this change. However, users on 64-bit platforms may experience
some problems using the `reset` management command.

Prior to this change, 64-bit platforms would generate a 64-bit, 16 character
digest in the constraint name; for example:

ALTER TABLE `myapp_sometable` ADD CONSTRAINT `object_id_refs_id_5e8f10c132091d1e` FOREIGN KEY ...

Following this change, all platforms, regardless of word size, will
generate a 32-bit, 8 character digest in the constraint name; for example:

ALTER TABLE `myapp_sometable` ADD CONSTRAINT `object_id_refs_id_32091d1e` FOREIGN KEY ...

As a result of this change, you will not be able to use the `reset`
management command on any table created with 64-bit constraints. This
is because the the new generated name will not match the historically
generated name; as a result, the SQL constructed by the `reset` command
will be invalid.

If you need to reset an application that was created with 64-bit
constraints, you will need to manually drop the old constraint prior
to invoking `reset`.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10966 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 64be8f29d5a9892047ac40d9306015ac402b1a39 1 parent 031385e
Russell Keith-Magee authored June 09, 2009

Showing 1 changed file with 7 additions and 4 deletions. Show diff stats Hide diff stats

  1. 11  django/db/backends/creation.py
11  django/db/backends/creation.py
@@ -25,6 +25,10 @@ class BaseDatabaseCreation(object):
25 25
     def __init__(self, connection):
26 26
         self.connection = connection
27 27
 
  28
+    def _digest(self, *args):
  29
+        "Generate a 32 bit digest of a set of arguments that can be used to shorten identifying names"
  30
+        return '%x' % (abs(hash(args)) % (1<<32))
  31
+
28 32
     def sql_create_model(self, model, style, known_models=set()):
29 33
         """
30 34
         Returns the SQL required to create a single model, as a tuple of:
@@ -128,7 +132,7 @@ def sql_for_pending_references(self, model, style, pending_references):
128 132
                 col = opts.get_field(f.rel.field_name).column
129 133
                 # For MySQL, r_name must be unique in the first 64 characters.
130 134
                 # So we are careful with character usage here.
131  
-                r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
  135
+                r_name = '%s_refs_%s_%s' % (r_col, col, self._digest(r_table, table))
132 136
                 final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
133 137
                     (qn(r_table), qn(truncate_name(r_name, self.connection.ops.max_name_length())),
134 138
                     qn(r_col), qn(table), qn(col),
@@ -187,8 +191,7 @@ def sql_for_many_to_many_field(self, model, f, style):
187 191
             output.append('\n'.join(table_output))
188 192
 
189 193
             for r_table, r_col, table, col in deferred:
190  
-                r_name = '%s_refs_%s_%x' % (r_col, col,
191  
-                        abs(hash((r_table, table))))
  194
+                r_name = '%s_refs_%s_%s' % (r_col, col, self._digest(r_table, table))
192 195
                 output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' %
193 196
                 (qn(r_table),
194 197
                 qn(truncate_name(r_name, self.connection.ops.max_name_length())),
@@ -289,7 +292,7 @@ def sql_remove_table_constraints(self, model, references_to_delete, style):
289 292
             col = f.column
290 293
             r_table = model._meta.db_table
291 294
             r_col = model._meta.get_field(f.rel.field_name).column
292  
-            r_name = '%s_refs_%s_%x' % (col, r_col, abs(hash((table, r_table))))
  295
+            r_name = '%s_refs_%s_%s' % (col, r_col, self._digest(table, r_table))
293 296
             output.append('%s %s %s %s;' % \
294 297
                 (style.SQL_KEYWORD('ALTER TABLE'),
295 298
                 style.SQL_TABLE(qn(table)),

0 notes on commit 64be8f2

Please sign in to comment.
Something went wrong with that request. Please try again.