<?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>unsignedint &#187; MySQL</title>
	<atom:link href="http://unsignedint.net/blog/category/development/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://unsignedint.net/blog</link>
	<description>adding noise to the signal</description>
	<lastBuildDate>Sat, 10 Jul 2010 10:32:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>MySQL Error: #1005 &#8211; Can&#039;t create table &#039;&#8230;.#sql-e7c_89.frm&#039; (errno: 150)</title>
		<link>http://unsignedint.net/blog/2009/08/01/mysql-error-1005-cant-create-table-sql-e7c_89-frm-errno-150/</link>
		<comments>http://unsignedint.net/blog/2009/08/01/mysql-error-1005-cant-create-table-sql-e7c_89-frm-errno-150/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 23:28:04 +0000</pubDate>
		<dc:creator>unsignedint</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[innodb]]></category>

		<guid isPermaLink="false">http://unsignedint.net/blog/?p=40</guid>
		<description><![CDATA[Se estiverem a tentar criar uma relação entre duas tabelas usando o MySQL e obtiverem o seguinte erro: #1005 - Can't create table '.\twitta\#sql-e7c_89.frm' (errno: 150) Experimentem activar o modo de debug do motor de innoDB de forma a obter mais informações sobre o erro e a poderem-no perceber. Para tal digitem na vossa consola: [...]]]></description>
			<content:encoded><![CDATA[<p>Se estiverem a tentar criar uma relação entre duas tabelas usando o MySQL e obtiverem o seguinte erro:</p>
<pre name="code" class="sql">
#1005 - Can't create table '.\twitta\#sql-e7c_89.frm' (errno: 150)
</pre>
<p>
Experimentem activar o modo de debug do motor de <strong>innoDB</strong> de forma a obter mais informações sobre o erro e a poderem-no perceber.<br />
<span id="more-40"></span><br />
Para tal digitem na vossa consola:</p>
<pre name="code" class="sql">
mysql> SHOW ENGINE INNODB STATUS;
</pre>
<p>E agora fazendo scroll para para cima sobre o output apresentado podem ver uma mensagem de erro mais detalhada. no meu caso foi:<br />
</p>
<pre name="code" class="sql">
------------------------
LATEST FOREIGN KEY ERROR
------------------------
090801 23:41:40 Error in foreign key constraint of table twitta/#sql-e7c_8a:
 FOREIGN KEY ( `user_id` ) REFERENCES `users` ( `id` ) ON DELETE CASCADE:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
</pre>
<p>Depois de consultar a documentação que o modo de debug sugere, rapidamente me apercebi do meu erro. A tabela onde estava criar a chave estrangeira o campo referenciado não estava indexado. Como se pode ler na documentação:</p>
<pre>
InnoDB requires indexes on foreign keys and referenced keys so that foreign key
checks can be fast and not require a table scan. In the referencing table,
there must be an index where the foreign key columns are listed as the first
columns in the same order. Such an index is created on the referencing table
automatically if it does not exist. (This is in contrast to some older versions,
in which indexes had to be created explicitly or the creation of foreign key
constraints would fail.) index_name, if given, is used as described previously
</pre>
<p>Espero que isto vós ajude.</p>
<div class="shr-publisher-40"></div>]]></content:encoded>
			<wfw:commentRss>http://unsignedint.net/blog/2009/08/01/mysql-error-1005-cant-create-table-sql-e7c_89-frm-errno-150/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

