From 9c38aab5be1704a77eef16af1e1031031a01fba0 Mon Sep 17 00:00:00 2001 From: Chauncey McAskill Date: Tue, 3 Mar 2020 18:12:51 -0500 Subject: [PATCH] Fix primary key for SQLite database In SQLite, `INT AUTO_INCREMENT` does not work; column must define `INTEGER PRIMARY KEY` to assign auto-increment behaviour. --- src/Charcoal/Source/DatabaseSource.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Charcoal/Source/DatabaseSource.php b/src/Charcoal/Source/DatabaseSource.php index 839574a7..19b4d4e4 100644 --- a/src/Charcoal/Source/DatabaseSource.php +++ b/src/Charcoal/Source/DatabaseSource.php @@ -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...) */