Author: russellm
Date: 2009-06-09 07:59:41 -0500 (Tue, 09 Jun 2009)
New Revision: 10966

Modified:
   django/trunk/django/db/backends/creation.py
Log:
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`.

Modified: django/trunk/django/db/backends/creation.py
===================================================================
--- django/trunk/django/db/backends/creation.py 2009-06-09 11:31:27 UTC (rev 
10965)
+++ django/trunk/django/db/backends/creation.py 2009-06-09 12:59:41 UTC (rev 
10966)
@@ -25,6 +25,10 @@
     def __init__(self, connection):
         self.connection = connection
 
+    def _digest(self, *args):
+        "Generate a 32 bit digest of a set of arguments that can be used to 
shorten identifying names"
+        return '%x' % (abs(hash(args)) % (1<<32))
+
     def sql_create_model(self, model, style, known_models=set()):
         """
         Returns the SQL required to create a single model, as a tuple of:
@@ -128,7 +132,7 @@
                 col = opts.get_field(f.rel.field_name).column
                 # For MySQL, r_name must be unique in the first 64 characters.
                 # So we are careful with character usage here.
-                r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, 
table))))
+                r_name = '%s_refs_%s_%s' % (r_col, col, self._digest(r_table, 
table))
                 final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s 
ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
                     (qn(r_table), qn(truncate_name(r_name, 
self.connection.ops.max_name_length())),
                     qn(r_col), qn(table), qn(col),
@@ -187,8 +191,7 @@
             output.append('\n'.join(table_output))
 
             for r_table, r_col, table, col in deferred:
-                r_name = '%s_refs_%s_%x' % (r_col, col,
-                        abs(hash((r_table, table))))
+                r_name = '%s_refs_%s_%s' % (r_col, col, self._digest(r_table, 
table))
                 output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD 
CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' %
                 (qn(r_table),
                 qn(truncate_name(r_name, 
self.connection.ops.max_name_length())),
@@ -289,7 +292,7 @@
             col = f.column
             r_table = model._meta.db_table
             r_col = model._meta.get_field(f.rel.field_name).column
-            r_name = '%s_refs_%s_%x' % (col, r_col, abs(hash((table, 
r_table))))
+            r_name = '%s_refs_%s_%s' % (col, r_col, self._digest(table, 
r_table))
             output.append('%s %s %s %s;' % \
                 (style.SQL_KEYWORD('ALTER TABLE'),
                 style.SQL_TABLE(qn(table)),


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to