Skip to content

Commit

Permalink
Fix primary key for SQLite database
Browse files Browse the repository at this point in the history
In SQLite, `INT AUTO_INCREMENT` does not work; column must define `INTEGER PRIMARY KEY` to assign auto-increment behaviour.
  • Loading branch information
mcaskill committed Mar 3, 2020
1 parent 7771bd4 commit 9c38aab
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Charcoal/Source/DatabaseSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ public function createTable()

$key = $model->key();
if ($key) {
$query .= ', PRIMARY KEY (`'.$key.'`) '."\n";
if ($driver === self::SQLITE_DRIVER_NAME) {
/** Convert MySQL syntax to SQLite */
$query = preg_replace('/`'.$key.'` INT(EGER)? AUTO_INCREMENT/', '`'.$key.'` INTEGER PRIMARY KEY', $query, 1);
} else {
$query .= ', PRIMARY KEY (`'.$key.'`) '."\n";
}
}

/** @todo Add indexes for all defined list constraints (yea... tough job...) */
Expand Down

0 comments on commit 9c38aab

Please sign in to comment.