mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Generated SQL should not contain multi-column primary keys
While totally valid in databases, the Schema system is not designed to handle multi-column primary keys in a sane way. In MySQL this results in two auto_increment columns, and in postgres this results in two serial columns. Fixes #3069
This commit is contained in:
parent
99813e97b3
commit
27e2132a13
2 changed files with 53 additions and 1 deletions
|
@ -2953,11 +2953,24 @@ class DboSource extends DataSource {
|
|||
$primary = null;
|
||||
$table = $this->fullTableName($curTable);
|
||||
|
||||
$primaryCount = 0;
|
||||
foreach ($columns as $col) {
|
||||
if (isset($col['key']) && $col['key'] === 'primary') {
|
||||
$primaryCount++;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($columns as $name => $col) {
|
||||
if (is_string($col)) {
|
||||
$col = array('type' => $col);
|
||||
}
|
||||
if (isset($col['key']) && $col['key'] === 'primary') {
|
||||
$isPrimary = isset($col['key']) && $col['key'] === 'primary';
|
||||
// Multi-column primary keys are not supported.
|
||||
if ($isPrimary && $primaryCount > 1) {
|
||||
unset($col['key']);
|
||||
$isPrimary = false;
|
||||
}
|
||||
if ($isPrimary) {
|
||||
$primary = $name;
|
||||
}
|
||||
if ($name !== 'indexes' && $name !== 'tableParameters') {
|
||||
|
|
|
@ -838,6 +838,45 @@ class MysqlTest extends CakeTestCase {
|
|||
$this->assertEquals('Test Comment', $result['other_col']['comment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that two columns with key => primary doesn't create invalid sql.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testTwoColumnsWithPrimaryKey() {
|
||||
$schema = new CakeSchema(array(
|
||||
'connection' => 'test',
|
||||
'roles_users' => array(
|
||||
'role_id' => array(
|
||||
'type' => 'integer',
|
||||
'null' => false,
|
||||
'default' => null,
|
||||
'key' => 'primary'
|
||||
),
|
||||
'user_id' => array(
|
||||
'type' => 'integer',
|
||||
'null' => false,
|
||||
'default' => null,
|
||||
'key' => 'primary'
|
||||
),
|
||||
'indexes' => array(
|
||||
'user_role_index' => array(
|
||||
'column' => array('role_id', 'user_id'),
|
||||
'unique' => 1
|
||||
),
|
||||
'user_index' => array(
|
||||
'column' => 'user_id',
|
||||
'unique' => 0
|
||||
)
|
||||
),
|
||||
)
|
||||
));
|
||||
|
||||
$result = $this->Dbo->createSchema($schema);
|
||||
$this->assertContains('`role_id` int(11) NOT NULL,', $result);
|
||||
$this->assertContains('`user_id` int(11) NOT NULL,', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that listSources method sends the correct query and parses the result accordingly
|
||||
* @return void
|
||||
|
|
Loading…
Add table
Reference in a new issue