Skip to content

Commit

Permalink
Correctly generate bigint primary keys in sqlite.
Browse files Browse the repository at this point in the history
generate bigint primary keys correctly. Autoincrement cannot be set as
it only works with INTEGER columns[1]. I decided to use some string
manipulations as the entire SQL generation bits are a bit janky and I've
already re-written them for 3.0.

[1] https://www.sqlite.org/autoinc.html

Closes #GH-1552
  • Loading branch information
markstory committed Aug 27, 2013
1 parent 1777c4f commit c1ae41d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -407,10 +407,19 @@ public function buildColumn($column) {
return null;
}

if (isset($column['key']) && $column['key'] === 'primary' && $type === 'integer') {
$isPrimary = (isset($column['key']) && $column['key'] === 'primary');
if ($isPrimary && $type === 'integer') {
return $this->name($name) . ' ' . $this->columns['primary_key']['name'];
}
return parent::buildColumn($column);
$out = parent::buildColumn($column);
if ($isPrimary && $type === 'biginteger') {
$replacement = 'PRIMARY KEY';
if ($column['null'] === false) {
$replacement = 'NOT NULL ' . $replacement;
}
return str_replace($this->columns['primary_key']['name'], $replacement, $out);
}
return $out;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -264,6 +264,17 @@ public function testBuildColumn() {
$result = $this->Dbo->buildColumn($data);
$expected = '"huge" bigint(20) NOT NULL';
$this->assertEquals($expected, $result);

$data = array(
'name' => 'id',
'type' => 'biginteger',
'length' => 20,
'null' => false,
'key' => 'primary',
);
$result = $this->Dbo->buildColumn($data);
$expected = '"id" bigint(20) NOT NULL PRIMARY KEY';
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit c1ae41d

Please sign in to comment.