<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>geek scrap &#187; dmigrations</title>
	<atom:link href="http://geekscrap.com/tags/dmigrations/feed/" rel="self" type="application/rss+xml" />
	<link>http://geekscrap.com</link>
	<description>there is at least one way to do it</description>
	<lastBuildDate>Tue, 12 Apr 2011 10:14:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Django and mysql names</title>
		<link>http://geekscrap.com/2010/01/django-and-mysql-names/</link>
		<comments>http://geekscrap.com/2010/01/django-and-mysql-names/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 17:15:22 +0000</pubDate>
		<dc:creator>geekscrap</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[How-tos]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[dmigrations]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://geekscrap.com/?p=70</guid>
		<description><![CDATA[Using django 1.1.1 framework with dmigrations tool, I&#8217;ve discovered that they don&#8217;t honour mysql naming conventions correctly. In fact, mysql has a limit of 64 characters on table names, index names and column names, but django doesn&#8217;t take it into account when generating tables and indices from models. In vanilla django, using &#8220;manage.py sql&#8221; or [...]]]></description>
			<content:encoded><![CDATA[<p>Using <a rel="nofollow" href="http://www.djangoproject.com/">django</a> 1.1.1 framework with <a rel="nofollow" href="http://code.google.com/p/dmigrations/">dmigrations</a> tool, I&#8217;ve discovered that they don&#8217;t honour mysql naming conventions correctly. In fact, mysql has a limit of 64 characters on table names, index names and column names, but django doesn&#8217;t take it into account when generating tables and indices from models.</p>
<p><span id="more-70"></span>In vanilla django, using &#8220;manage.py sql&#8221; or &#8220;manage.py sqlindexes&#8221; you can generate respectively &#8220;CREATE TABLE&#8221; and &#8220;CREATE INDEX&#8221; sql statements to setup your database schema (if you use manage.py syncdb, generated sql is feeded into your database). Table names are defined in <em>db_table</em> variable in model&#8217;s meta class and default name is generated using the following statement in django.db.models.options:</p>
<pre lang="python">self.db_table = "%s_%s" % (self.app_label, self.module_name)</pre>
<p>Now, if your generated db_table is greater than 64 characters (quite easy if your application name is long), django doesn&#8217;t truncate it and when sql is feeded into mysql, table creation routine will fail. A similar trouble happens for index creation routine (typically used with <em>unique</em> constraints) and using dmigrations tool instead of plain syncdb doesn&#8217;t get any better.</p>
<p>At this point, there are two options for table naming:</p>
<ol>
<li>Renaming tables in models.py by hand (quite boring if your project is big).</li>
<li>Patching django to apply a truncation to db_table.</li>
</ol>
<p>For option 2, here&#8217;s the simple one-liner patch you need for django 1.1.1:</p>
<pre lang="patch">--- django/db/backends/mysql/base.py.orig
+++ django/db/backends/mysql/base.py
@@ -214,6 +214,9 @@
         second = '%s-12-31 23:59:59.99'
         return [first % value, second % value]

+    def max_name_length(self):
+        return 64
+
 class DatabaseWrapper(BaseDatabaseWrapper):

     operators = {</pre>
<p>In dmigrations, index names are defined as:</p>
<pre lang="python">index_name = '%s_%s_%s' % (app, model, column)</pre>
<p>You can fix the length problem in r29 with this simple patch:</p>
<pre lang="patch">--- dmigrations/mysql/migrations.py.orig
+++ dmigrations/mysql/migrations.py
@@ -141,7 +141,7 @@
     def __init__(self, app, model, column):
         model = model.lower()
         self.app, self.model, self.column = app, model, column
-        index_name = '%s_%s_%s' % (app, model, column)
+        index_name = '%s_refs_id_%s' % (column, abs(hash((app, model, column))))
         super(AddIndex, self).__init__(
             sql_up = [self.add_index_sql % (index_name, app, model, column)],
             sql_down = [self.drop_index_sql % (app, model, index_name)],</pre>
]]></content:encoded>
			<wfw:commentRss>http://geekscrap.com/2010/01/django-and-mysql-names/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
