[Web] Fix database init

master
André 2018-08-15 14:16:55 +02:00
parent 1791383eab
commit 2c58323e36
1 changed files with 6 additions and 2 deletions

View File

@ -759,8 +759,12 @@ function init_db_schema() {
if ($num_results == 0) { if ($num_results == 0) {
if (strpos($type, 'AUTO_INCREMENT') !== false) { if (strpos($type, 'AUTO_INCREMENT') !== false) {
$type = $type . ' PRIMARY KEY '; $type = $type . ' PRIMARY KEY ';
// Adding an AUTO_INCREMENT key, need to drop primary keys first // Adding an AUTO_INCREMENT key, need to drop primary keys first, if exists
$pdo->query("ALTER TABLE `" . $table . "` DROP PRIMARY KEY"); $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); $pdo->query("ALTER TABLE `" . $table . "` ADD `" . $column . "` " . $type);
} }