diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index ea1c7dbca02..ef4bab7e743 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -2984,7 +2984,7 @@ public function createSchema($schema, $tableName = null) { $tableParameters = array_merge($tableParameters, $this->buildTableParameters($col, $table)); } } - if (empty($indexes) && !empty($primary)) { + if (!isset($columns['indexes']['PRIMARY']) && !empty($primary)) { $col = array('PRIMARY' => array('column' => $primary, 'unique' => 1)); $indexes = array_merge($indexes, $this->buildIndex($col, $table)); } diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index f14bf611682..4f3baa0bdf2 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -877,6 +877,52 @@ public function testTwoColumnsWithPrimaryKey() { $this->assertContains('`user_id` int(11) NOT NULL,', $result); } +/** + * Test that the primary flag is handled correctly. + * + * @return void + */ + public function testCreateSchemaAutoPrimaryKey() { + $schema = new CakeSchema(); + $schema->tables = array( + 'no_indexes' => array( + 'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'), + 'data' => array('type' => 'integer', 'null' => false), + 'indexes' => array(), + ) + ); + $result = $this->Dbo->createSchema($schema, 'no_indexes'); + $this->assertContains('PRIMARY KEY (`id`)', $result); + $this->assertNotContains('UNIQUE KEY', $result); + + $schema->tables = array( + 'primary_index' => array( + 'id' => array('type' => 'integer', 'null' => false), + 'data' => array('type' => 'integer', 'null' => false), + 'indexes' => array( + 'PRIMARY' => array('column' => 'id', 'unique' => 1), + 'some_index' => array('column' => 'data', 'unique' => 1) + ), + ) + ); + $result = $this->Dbo->createSchema($schema, 'primary_index'); + $this->assertContains('PRIMARY KEY (`id`)', $result); + $this->assertContains('UNIQUE KEY `some_index` (`data`)', $result); + + $schema->tables = array( + 'primary_flag_has_index' => array( + 'id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'), + 'data' => array('type' => 'integer', 'null' => false), + 'indexes' => array ( + 'some_index' => array('column' => 'data', 'unique' => 1) + ), + ) + ); + $result = $this->Dbo->createSchema($schema, 'primary_flag_has_index'); + $this->assertContains('PRIMARY KEY (`id`)', $result); + $this->assertContains('UNIQUE KEY `some_index` (`data`)', $result); + } + /** * Tests that listSources method sends the correct query and parses the result accordingly * @return void