Skip to content

Commit 3849df0

Browse files
fix duplicate primary keys for tables without models
1 parent 8d0e1fa commit 3849df0

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

lib/Cake/Model/CakeSchema.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,16 @@ protected function _columns(&$Obj) {
613613
$db = $Obj->getDataSource();
614614
$fields = $Obj->schema(true);
615615

616+
$hasPrimaryAlready = false;
617+
foreach ($fields as $value) {
618+
if (isset($value['key']) && $value['key'] === 'primary') {
619+
$hasPrimaryAlready = true;
620+
}
621+
}
622+
616623
$columns = array();
617624
foreach ($fields as $name => $value) {
618-
if ($Obj->primaryKey === $name) {
625+
if ($Obj->primaryKey === $name && !$hasPrimaryAlready && !isset($value['key'])) {
619626
$value['key'] = 'primary';
620627
}
621628
if (!isset($db->columns[$value['type']])) {

lib/Cake/Test/Case/Model/CakeSchemaTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,39 @@ class SchemaCrossDatabaseFixture extends CakeTestFixture {
363363
);
364364
}
365365

366+
/**
367+
* NonConventionalPrimaryKeyFixture class
368+
*
369+
* @package Cake.Test.Case.Model
370+
*/
371+
class NonConventionalPrimaryKeyFixture extends CakeTestFixture {
372+
373+
/**
374+
* name property
375+
*
376+
* @var string
377+
*/
378+
public $name = 'NonConventional';
379+
380+
/**
381+
* table property
382+
*
383+
* @var string
384+
*/
385+
public $table = 'non_conventional';
386+
387+
/**
388+
* fields property
389+
*
390+
* @var array
391+
*/
392+
public $fields = array(
393+
'version_id' => array('type' => 'integer', 'key' => 'primary'),
394+
'id' => array('type' => 'integer'),
395+
'name' => 'string'
396+
);
397+
}
398+
366399
/**
367400
* SchemaPrefixAuthUser class
368401
*
@@ -649,6 +682,36 @@ public function testSchemaReadWithCrossDatabase() {
649682
$fixture->drop($db);
650683
}
651684

685+
/**
686+
* testSchemaRead method when a primary key is on a non-conventional column
687+
*
688+
* @return void
689+
*/
690+
public function testSchemaReadWithNonConventionalPrimaryKey() {
691+
$db = ConnectionManager::getDataSource('test');
692+
$fixture = new NonConventionalPrimaryKeyFixture();
693+
$fixture->create($db);
694+
695+
$read = $this->Schema->read(array(
696+
'connection' => 'test',
697+
'name' => 'TestApp',
698+
'models' => false
699+
));
700+
$fixture->drop($db);
701+
702+
$has_table = isset($read['tables']['non_conventional']);
703+
$this->assertTrue($has_table, 'non_conventional table should appear');
704+
if ($has_table) {
705+
$version_id_has_key = isset($read['tables']['non_conventional']['version_id']['key']);
706+
$this->assertTrue($version_id_has_key, 'version_id key should be set');
707+
if ($version_id_has_key) {
708+
$version_id_key_is_primary = $read['tables']['non_conventional']['version_id']['key'] === 'primary';
709+
$this->assertTrue($version_id_key_is_primary, 'version_id key should be primary');
710+
}
711+
$this->assertFalse(isset($read['tables']['non_conventional']['id']['key']), 'id key should not be set');
712+
}
713+
}
714+
652715
/**
653716
* test that tables are generated correctly
654717
*

0 commit comments

Comments
 (0)