database-command icon indicating copy to clipboard operation
database-command copied to clipboard

composite primary key

Open schmunk42 opened this issue 12 years ago • 2 comments

When the primary key is composed of two columns, the command generation is also incorrect:

CREATE TABLE IF NOT EXISTS `alert_listener` (
  `alert_listener_id` int(11) NOT NULL AUTO_INCREMENT,
  `listener` varchar(250) DEFAULT NULL COMMENT 'Describes the listner (email address, phone number, ...).',
  `listener_type` int(11) DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT NULL,
  `entity_alert_id` int(11) NOT NULL,
  PRIMARY KEY (`alert_listener_id`,`entity_alert_id`),
  KEY `fk_alert_listener_device_alert1` (`entity_alert_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=815 ;

gives

$this->createTable(AlertListener::model()->tableName(),
                array(
                        "alert_listener_id"=>"pk",
                        "listener"=>"varchar(250)",
                        "listener_type"=>"int(11)",
                        "is_active"=>"tinyint(1)",
                        "entity_alert_id"=>"int(11) NOT NULL",
                        "PRIMARY KEY (entity_alert_id)"
                ),
                $options);

schmunk42 avatar Jan 18 '13 14:01 schmunk42

I my case: Table in sql: CREATE TABLE IF NOT EXISTS AuthAssignment ( itemname varchar(64) NOT NULL, userid varchar(64) NOT NULL, bizrule text, data text, PRIMARY KEY (itemname,userid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

migrate code from you ext: // Schema for table 'AuthAssignment' $this->createTable("AuthAssignment", array( "itemname"=>"varchar(64) NOT NULL PRIMARY KEY", "userid"=>"varchar(64) NOT NULL PRIMARY KEY", "bizrule"=>"text", "data"=>"text", "PRIMARY KEY (itemname)" ), $options);

var_dump:

for field itemname: ["isPrimaryKey"]=> bool(true) ["isForeignKey"]=> bool(true)

for field userid: ["isPrimaryKey"]=> bool(true) ["isForeignKey"]=> bool(false)

Then valid code that should be generated is:

$this->createTable("AuthAssignment", array( "itemname"=>"varchar(64) NOT NULL UNIQUE", "userid"=>"varchar(64) NOT NULL", "bizrule"=>"text", "data"=>"text", "PRIMARY KEY (itemname, userid)" ), $options);

so algorithm will be like this (less or more): IF in THE SAME table present more than one ["isPrimaryKey"]=>TRUE THEN FIRST NOT NULL UNIQUE", SECOND "PRIMARY KEY (FIRS, SECOND)"

right ?? If yest could you make appropriate changes ?

regards bw

bwx avatar Jan 22 '13 20:01 bwx

Sorry, I have no time at the moment to implement this feature, but feel free to send me a pull request.

schmunk42 avatar Jan 23 '13 15:01 schmunk42