<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Sat May 18 20:05:01 UTC 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://www.doctrine-project.org/jira/si/jira.issueviews:issue-xml/DC-368/DC-368.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Doctrine Project</title>
    <link>http://www.doctrine-project.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>5.2.7</version>
        <build-number>850</build-number>
        <build-date>21-02-2013</build-date>
    </build-info>

<item>
            <title>[DC-368] createTablesFromModels() wants to create already existing FOREIGN KEY constraints</title>
                <link>http://www.doctrine-project.org/jira/browse/DC-368</link>
                <project id="10031" key="DC">Doctrine 1</project>
                        <description>&lt;p&gt;If you create InnoDB tables from a given model directory, the table generator wants to create a foreign key constraint that already exists.&lt;/p&gt;

&lt;p&gt;1. Doctrine_Core::generateModelsFromYaml(&apos;yaml&apos;, &apos;models&apos;);&lt;br/&gt;
2. Doctrine_Core::createTablesFromModels(&apos;models&apos;);&lt;br/&gt;
3. Running this setup twice, will print out the following error:&lt;/p&gt;

&lt;p&gt;SQLSTATE&lt;span class=&quot;error&quot;&gt;&amp;#91;HY000&amp;#93;&lt;/span&gt;: General error: 1005 Can&apos;t create table &apos;./dev_mreiche/#sql-930_2cb7.frm&apos; (errno: 121). Failing Query: &quot;ALTER TABLE group_user ADD CONSTRAINT group_user_user_id_user_id FOREIGN KEY (user_id) REFERENCES user(id)&quot;. Failing Query: ALTER TABLE group_user ADD CONSTRAINT group_user_user_id_user_id FOREIGN KEY (user_id) REFERENCES user(id)&lt;/p&gt;</description>
                <environment>mysql  Ver 14.12 Distrib 5.0.75, for debian-linux-gnu (i486) using readline 5.2&lt;br/&gt;
PHP 5.2.11-0.dotdeb.1 with Suhosin-Patch 0.9.7 (cli) (built: Sep 20 2009 09:41:43)&lt;br/&gt;
Ubuntu 9.04&lt;br/&gt;
</environment>
            <key id="10632">DC-368</key>
            <summary>createTablesFromModels() wants to create already existing FOREIGN KEY constraints</summary>
                <type id="1" iconUrl="http://www.doctrine-project.org/jira/images/icons/issuetypes/bug.png">Bug</type>
                                <priority id="3" iconUrl="http://www.doctrine-project.org/jira/images/icons/priorities/major.png">Major</priority>
                    <status id="1" iconUrl="http://www.doctrine-project.org/jira/images/icons/statuses/open.png">Open</status>
                    <resolution id="-1">Unresolved</resolution>
                                <assignee username="jwage">Jonathan H. Wage</assignee>
                                <reporter username="mreiche">Mike Reiche</reporter>
                        <labels>
                    </labels>
                <created>Fri, 18 Dec 2009 13:35:19 +0000</created>
                <updated>Tue, 24 Aug 2010 04:32:21 +0000</updated>
                                    <version>1.2.1</version>
                                                <component>Import/Export</component>
                        <due></due>
                    <votes>1</votes>
                        <watches>2</watches>
                        <comments>
                    <comment id="11228" author="mreiche" created="Fri, 18 Dec 2009 13:38:50 +0000"  >&lt;p&gt;Moved model and setup code from description to attachment.&lt;/p&gt;</comment>
                    <comment id="11636" author="m.olszewski" created="Tue, 9 Feb 2010 08:53:02 +0000"  >&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;Just a quick comment - the test case you&apos;ve provided works fine for me using 1.2.1 and also latest code from 1.2 branch... you might try that.&lt;/p&gt;

&lt;p&gt;The error above (1005) is caused by mismatch between local and foreign key definitions. So it seems somewhere along the lines you/Doctrine messed up field definitions (although I can&apos;t see it provided test case). Try using integer without length definition in YAML, like:&lt;/p&gt;

&lt;p&gt;id:&lt;br/&gt;
      type: integer&lt;br/&gt;
      autoincrement: true&lt;br/&gt;
      primary: true&lt;/p&gt;

&lt;p&gt;and check if this is working.&lt;/p&gt;</comment>
                    <comment id="11886" author="ppw8bnh3jy2a" created="Mon, 1 Mar 2010 14:44:47 +0000"  >&lt;p&gt;I&apos;m having the same issue.  However, I&apos;ve tried the test case using a sqlite memory only database and i can&apos;t trigger the problem.&lt;/p&gt;

&lt;p&gt;Here&apos;s my mysql schema, for your review - which does trigger the issue using 1.2.1.&lt;/p&gt;

</comment>
                    <comment id="11887" author="ppw8bnh3jy2a" created="Mon, 1 Mar 2010 14:48:51 +0000"  >&lt;p&gt;I put some debugging statements in Doctrine/Export.php around line 1216:&lt;br/&gt;
------------&lt;br/&gt;
                      $connection-&amp;gt;exec($query);&lt;br/&gt;
                  } catch (Doctrine_Connection_Exception $e) {&lt;br/&gt;
                      // we only want to silence table already exists errors&lt;br/&gt;
+                     print_r($e-&amp;gt;getPortableCode());&lt;br/&gt;
+                     print_r(Doctrine_Core::ERR_ALREADY_EXISTS);&lt;br/&gt;
+                     print_r($e-&amp;gt;getMessage());&lt;br/&gt;
+                     echo &quot;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&quot;;&lt;br/&gt;
+//                                  exit;&lt;br/&gt;
+&lt;br/&gt;
+                     &lt;br/&gt;
                      if ($e-&amp;gt;getPortableCode() !== Doctrine_Core::ERR_ALREADY_EXISTS) {&lt;br/&gt;
                          $connection-&amp;gt;rollback();&lt;/p&gt;

&lt;p&gt;----------------------&lt;/p&gt;


&lt;p&gt;Here&apos;s the output I get:&lt;/p&gt;

&lt;p&gt;-5-5SQLSTATE&lt;span class=&quot;error&quot;&gt;&amp;#91;42S01&amp;#93;&lt;/span&gt;: Base table or view already exists: 1050 Table &apos;category&apos; already exists. Failing Query: &quot;CREATE TABLE category (id BIGINT AUTO_INCREMENT, name VARCHAR(80), company_id BIGINT, INDEX company_id_idx (company_id), PRIMARY KEY(id)) ENGINE = INNODB&quot;&lt;/p&gt;

&lt;p&gt;-5-5SQLSTATE&lt;span class=&quot;error&quot;&gt;&amp;#91;42S01&amp;#93;&lt;/span&gt;: Base table or view already exists: 1050 Table &apos;company&apos; already exists. Failing Query: &quot;CREATE TABLE company (id BIGINT AUTO_INCREMENT, name VARCHAR(80) UNIQUE, address1 VARCHAR(80), address2 VARCHAR(80), city VARCHAR(80), state VARCHAR(10), zipcode VARCHAR(20), phone VARCHAR(20), fax VARCHAR(20), url VARCHAR(255), logo VARCHAR(255), perferedcolumn BIGINT, randomsort BIGINT, comment LONGTEXT, PRIMARY KEY(id)) ENGINE = INNODB&quot;&lt;/p&gt;

&lt;p&gt;-5-5SQLSTATE&lt;span class=&quot;error&quot;&gt;&amp;#91;42S01&amp;#93;&lt;/span&gt;: Base table or view already exists: 1050 Table &apos;user&apos; already exists. Failing Query: &quot;CREATE TABLE user (email VARCHAR(255), password VARCHAR(80), fullname VARCHAR(80), company_id BIGINT, cellphone VARCHAR(20), voice VARCHAR(20), voiceext VARCHAR(20), fax VARCHAR(20), comment LONGTEXT, status TINYINT(1), admin TINYINT(1), timeadded DATETIME, timechagned DATETIME, INDEX company_id_idx (company_id), PRIMARY KEY(email)) ENGINE = INNODB&quot;&lt;/p&gt;

&lt;p&gt;-15-5SQLSTATE&lt;span class=&quot;error&quot;&gt;&amp;#91;HY000&amp;#93;&lt;/span&gt;: General error: 1005 Can&apos;t create table &apos;./iprfpartners/#sql-7eeb_2d8.frm&apos; (errno: 121). Failing Query: &quot;ALTER TABLE category ADD CONSTRAINT category_company_id_company_id FOREIGN KEY (company_id) REFERENCES company(id)&quot;&lt;/p&gt;

</comment>
                    <comment id="14037" author="aristondarmayuda" created="Mon, 23 Aug 2010 16:12:37 +0000"  >&lt;p&gt;I have same problem, but this problem is face on db engine side. When we look at the last query sql like:&lt;/p&gt;

&lt;p&gt;ALTER TABLE category ADD CONSTRAINT category_company_id_company_id FOREIGN KEY (company_id) REFERENCES company(id)&lt;/p&gt;

&lt;p&gt;The first call of createTablesFromModels will create the table and add contraint which run fine because table and constraint not exist yet. Then when we execute createTablesFromModels again it will fail because it still try to create constraint which already exist on table. I&apos;m still looking on MySql section how to check the constrain if it exist or not. If the constraint not exist then we can execute the sql above, if not we need to avoid execution for sql above, this mean we cannot MODIFY the constrain (like ON UPDATE and ON DELETE mode).&lt;/p&gt;</comment>
                    <comment id="14044" author="aristondarmayuda" created="Tue, 24 Aug 2010 04:29:52 +0000"  >&lt;p&gt;I&apos;ve figure out the errorno: 121 which I thinking is that the process try re-adding existing CONSTRAINT name. The resolve that I&apos;ve found is for MySQL engine and modify some Doctrine libraries.&lt;/p&gt;

&lt;p&gt;1. I&apos;ve modify Doctrine_Export_Mysql by adding override function of:&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;&lt;span class=&quot;code-keyword&quot;&gt;public&lt;/span&gt; function createForeignKeySql($table, array $definition)
{
    $tableInfo = $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;conn-&amp;gt;execute(&apos;SHOW CREATE TABLE &apos;.$&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;conn-&amp;gt;quoteIdentifier($table));
    &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ($tableInfo &amp;amp;&amp;amp; ($row = $tableInfo-&amp;gt;fetch()))
      &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ($row[&apos;Create Table&apos;] &amp;amp;&amp;amp; preg_match(&apos;/CONSTRAINT `&apos;.$definition[&apos;name&apos;].&apos;`.+/i&apos;, $row[&apos;Create Table&apos;])) &lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; &apos;&apos;;
    &lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; parent::createForeignKeySql($table, $definition);
}
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;2. Since if CONSTRAINT already exist the createForeignKeySql return empty string, we need modify function on Doctrine_Export with:&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-java&quot;&gt;&lt;span class=&quot;code-keyword&quot;&gt;public&lt;/span&gt; function createForeignKey($table, array $definition)
{
    $sql = $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;createForeignKeySql($table, $definition);
    &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ($sql != &apos;&apos;)
      &lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;conn-&amp;gt;execute($sql);
    &lt;span class=&quot;code-keyword&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; -1;
}
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now by add/modify function above I can savely run createTablesFromModels because if CONSTRAINT of a table exist on database the ALTER TABLE ... ADD CONSTRAINT will not generated. Hope this can help some of you guys.&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                    <attachment id="10404" name="schema.txt" size="4059" author="ppw8bnh3jy2a" created="Mon, 1 Mar 2010 14:44:47 +0000" />
                    <attachment id="10239" name="testcase.zip" size="723" author="mreiche" created="Fri, 18 Dec 2009 13:38:50 +0000" />
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>