Skip to content
Permalink
Browse files

Sqlite composite primary key support.

SQLite does not support autoincrement/serial on composite keys. Add reflection for the new autoIncrement, property.
There is no matching generation feature as SQLite does not support this kind of table generation.
  • Loading branch information...
markstory committed Jan 2, 2014
1 parent 3383ce1 commit 4d8b757a0f91bdabae044a5c78c086a740b7cdd3
Showing with 68 additions and 0 deletions.
  1. +1 −0 Cake/Database/Schema/SqliteSchema.php
  2. +67 −0 Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php
@@ -125,6 +125,7 @@ public function convertFieldDescription(Table $table, $row) {
];
if ($row['pk'] == true) {
$field['null'] = false;
$field['autoIncrement'] = true;
}
$table->addColumn($row['name'], $field);
if ($row['pk'] == true) {
@@ -230,6 +230,7 @@ public function testDescribeTable() {
'precision' => null,
'comment' => null,
'unsigned' => false,
'autoIncrement' => true,
],
'title' => [
'type' => 'string',
@@ -256,6 +257,7 @@ public function testDescribeTable() {
'unsigned' => false,
'precision' => null,
'comment' => null,
'autoIncrement' => null,
],
'published' => [
'type' => 'boolean',
@@ -681,6 +683,71 @@ public function testCreateSql() {
);
}
/**
* Test primary key generation & auto-increment.
*
* @return void
*/
public function testCreateSqlCompositeIntegerKey() {
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));
$table = (new Table('articles_tags'))
->addColumn('article_id', [
'type' => 'integer',
'null' => false
])
->addColumn('tag_id', [
'type' => 'integer',
'null' => false,
])
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['article_id', 'tag_id']
]);
$expected = <<<SQL
CREATE TABLE "articles_tags" (
"article_id" INTEGER NOT NULL,
"tag_id" INTEGER NOT NULL,
CONSTRAINT "primary" PRIMARY KEY ("article_id", "tag_id")
)
SQL;
$result = $table->createSql($connection);
$this->assertCount(1, $result);
$this->assertEquals($expected, $result[0]);
// Sqlite only supports AUTO_INCREMENT on single column primary
// keys. Ensure that schema data follows the limitations of Sqlite.
$table = (new Table('composite_key'))
->addColumn('id', [
'type' => 'integer',
'null' => false,
'autoIncrement' => true
])
->addColumn('account_id', [
'type' => 'integer',
'null' => false,
])
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id', 'account_id']
]);
$expected = <<<SQL
CREATE TABLE "composite_key" (
"id" INTEGER NOT NULL,
"account_id" INTEGER NOT NULL,
CONSTRAINT "primary" PRIMARY KEY ("id", "account_id")
)
SQL;
$result = $table->createSql($connection);
$this->assertCount(1, $result);
$this->assertEquals($expected, $result[0]);
}
/**
* test dropSql
*

0 comments on commit 4d8b757

Please sign in to comment.
You can’t perform that action at this time.