[DC-655] When Export from Models to database is made some constraints may not be created Created: 29/Apr/10  Updated: 11/Jun/10

Status: Open
Project: Doctrine 1
Component/s: Import/Export
Affects Version/s: 1.2.2
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Oleg Stepura Assignee: Jonathan H. Wage
Resolution: Unresolved Votes: 2
Labels: None
Environment:

FreeBSD 6.1, Apache 2.2.4, PHP 5.3.2, MySQL 5.0.41



 Description   

When exporting my schema from yaml I noticed that some CONSTRAINT are not created, but all the models are. After analyzing the code I found that an issue is not related to YAML but is related to Doctrine_Table::getExportableFormat(). I have models creation option to create it in PEAR-like style.

So, when you have a schema like this:

----------------
Partner_Code:
tableName: partner_code
columns:
id:
type: integer(4)
unsigned: true
primary: true
autoincrement: true
notnull: true
code:
type: string(64)
unique: true
notnull: true

Partner_Channel:
tableName: partner_channel
columns:
id:
type: integer(4)
unsigned: true
primary: true
autoincrement: true
notnull: true
name:
type: string(64)
notnull: true
relations:
Codes:
class: Partner_Code
local: channel_id
foreign: code_id
refClass: Partner_Channel_Code
foreignAlias: Partner_Channel
foreignType: one
type: many

Partner_Channel_Code:
tableName: partner_channel_x_code
columns:
channel_id:
type: integer(4)
unsigned: true
primary: true
notnull: true
code_id:
type: integer(4)
unsigned: true
primary: true
notnull: true
----------------

it generates sql like this:

----------------
CREATE TABLE partner_channel (id INT UNSIGNED AUTO_INCREMENT, name VARCHAR(64) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;
CREATE TABLE partner_channel_x_code (channel_id INT UNSIGNED, code_id INT UNSIGNED, PRIMARY KEY(channel_id, code_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;
CREATE TABLE partner_code (id INT UNSIGNED AUTO_INCREMENT, code VARCHAR(64) NOT NULL UNIQUE, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;
ALTER TABLE partner_channel_x_code ADD CONSTRAINT partner_channel_x_code_channel_id_partner_channel_id FOREIGN KEY (channel_id) REFERENCES partner_channel(id);
----------------

But should also generate:

----------------
ALTER TABLE partner_channel_x_code ADD CONSTRAINT partner_channel_x_code_code_id_partner_code_id FOREIGN KEY (code_id) REFERENCES partner_code(id);
----------------

But if I just change the schema with this changes (notice the Channel changed to Shannel):

----------------
Partner_Channel:
relations:
Codes:
refClass: Partner_Shannel_Code

Partner_Shannel_Code:
tableName: partner_channel_x_code
----------------

Everything is created.

----------------
CREATE TABLE partner_channel (id INT UNSIGNED AUTO_INCREMENT, name VARCHAR(64) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;
CREATE TABLE partner_code (id INT UNSIGNED AUTO_INCREMENT, code VARCHAR(64) NOT NULL UNIQUE, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;
CREATE TABLE partner_channel_x_code (channel_id INT UNSIGNED, code_id INT UNSIGNED, PRIMARY KEY(channel_id, code_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;
ALTER TABLE partner_channel_x_code ADD CONSTRAINT partner_channel_x_code_code_id_partner_code_id FOREIGN KEY (code_id) REFERENCES partner_code(id);
ALTER TABLE partner_channel_x_code ADD CONSTRAINT partner_channel_x_code_channel_id_partner_channel_id FOREIGN KEY (channel_id) REFERENCES partner_channel(id);
----------------

So as I figured out is that when creating the tables alphabetic order has an influence on creating the CONSTRAINT for that Model in sql. I suppose line 715 of the Doctrine/Table.php near $key = $this->_checkForeignKeyExists(...) is operating, but I am not sure (when inserting into DB some tables are not created yet?).

Do not know how to markup here, it's my first issue in Jira, please press "edit" to see the yaml code formatted



 Comments   
Comment by Oleg Stepura [ 29/Apr/10 ]

Also what I noticed today is that this YAML schema:

Partner_Channel:
tableName: partner_channel
comment: List of channels.

Does not add comment to the MySQL table when trying to do this through the cli build-all-load command. Neither the generated Model nor the exported sql chema have this option ('comment').

maybe this should be a different bug report...

Comment by Guilliam X [ 11/Jun/10 ]

For your missing table comment, try nesting "comment" into "options", like this:

YAML
Partner_Channel:
  tableName: partner_channel
  options:
    comment: List of channels.

Now for the missing constraints issue, I confirm! See #DC-502

Comment by Oleg Stepura [ 11/Jun/10 ]

Hi! As for the comments - your decision seems like a workaround. Is it so or is it the way it should work? Do you know how this kind of stuff will work in 2.0 version?

Generated at Sun Oct 26 06:13:22 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.