From 2c58323e36deb04575085c3021508a0cba26df88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Wed, 15 Aug 2018 14:16:55 +0200 Subject: [PATCH] [Web] Fix database init --- data/web/inc/init_db.inc.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data/web/inc/init_db.inc.php b/data/web/inc/init_db.inc.php index c99484d5..9c154e7f 100644 --- a/data/web/inc/init_db.inc.php +++ b/data/web/inc/init_db.inc.php @@ -759,8 +759,12 @@ function init_db_schema() { if ($num_results == 0) { if (strpos($type, 'AUTO_INCREMENT') !== false) { $type = $type . ' PRIMARY KEY '; - // Adding an AUTO_INCREMENT key, need to drop primary keys first - $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY"); + // Adding an AUTO_INCREMENT key, need to drop primary keys first, if exists + $stmt = $pdo->query("SHOW KEYS FROM `" . $table . "` WHERE Key_name = 'PRIMARY'"); + $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC)); + if ($num_results != 0) { + $pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY"); + } } $pdo->query("ALTER TABLE `" . $table . "` ADD `" . $column . "` " . $type); }