Skip to content

Commit a0a61b5

Browse files
committed
Fix issue where createSchema() would omit primary keys sometimes.
Fix missing primary key SQL when using the primary flag + other indexes. Fixes #3292
1 parent 1c0c860 commit a0a61b5

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/Cake/Model/Datasource/DboSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2984,7 +2984,7 @@ public function createSchema($schema, $tableName = null) {
29842984
$tableParameters = array_merge($tableParameters, $this->buildTableParameters($col, $table));
29852985
}
29862986
}
2987-
if (empty($indexes) && !empty($primary)) {
2987+
if (!isset($columns['indexes']['PRIMARY']) && !empty($primary)) {
29882988
$col = array('PRIMARY' => array('column' => $primary, 'unique' => 1));
29892989
$indexes = array_merge($indexes, $this->buildIndex($col, $table));
29902990
}

lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,52 @@ public function testTwoColumnsWithPrimaryKey() {
877877
$this->assertContains('`user_id` int(11) NOT NULL,', $result);
878878
}
879879

880+
/**
881+
* Test that the primary flag is handled correctly.
882+
*
883+
* @return void
884+
*/
885+
public function testCreateSchemaAutoPrimaryKey() {
886+
$schema = new CakeSchema();
887+
$schema->tables = array(
888+
'no_indexes' => array(
889+
'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
890+
'data' => array('type' => 'integer', 'null' => false),
891+
'indexes' => array(),
892+
)
893+
);
894+
$result = $this->Dbo->createSchema($schema, 'no_indexes');
895+
$this->assertContains('PRIMARY KEY (`id`)', $result);
896+
$this->assertNotContains('UNIQUE KEY', $result);
897+
898+
$schema->tables = array(
899+
'primary_index' => array(
900+
'id' => array('type' => 'integer', 'null' => false),
901+
'data' => array('type' => 'integer', 'null' => false),
902+
'indexes' => array(
903+
'PRIMARY' => array('column' => 'id', 'unique' => 1),
904+
'some_index' => array('column' => 'data', 'unique' => 1)
905+
),
906+
)
907+
);
908+
$result = $this->Dbo->createSchema($schema, 'primary_index');
909+
$this->assertContains('PRIMARY KEY (`id`)', $result);
910+
$this->assertContains('UNIQUE KEY `some_index` (`data`)', $result);
911+
912+
$schema->tables = array(
913+
'primary_flag_has_index' => array(
914+
'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
915+
'data' => array('type' => 'integer', 'null' => false),
916+
'indexes' => array (
917+
'some_index' => array('column' => 'data', 'unique' => 1)
918+
),
919+
)
920+
);
921+
$result = $this->Dbo->createSchema($schema, 'primary_flag_has_index');
922+
$this->assertContains('PRIMARY KEY (`id`)', $result);
923+
$this->assertContains('UNIQUE KEY `some_index` (`data`)', $result);
924+
}
925+
880926
/**
881927
* Tests that listSources method sends the correct query and parses the result accordingly
882928
* @return void

0 commit comments

Comments
 (0)