From a5951dc2ce6fdf2bca9091379f66238369c69297 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 8 Jun 2023 16:49:59 +0530 Subject: [PATCH] fix: disable/enable foreign key check only if it's enabled Signed-off-by: Pranav C --- .../src/db/sql-client/lib/sqlite/SqliteClient.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts b/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts index 49e078a1b0..00ecca611d 100644 --- a/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts +++ b/packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts @@ -22,7 +22,8 @@ class SqliteClient extends KnexClient { // sqlite does not support inserting default values and knex fires a warning without this flag connectionConfig.connection.useNullAsDefault = true; super(connectionConfig); - this.sqlClient = connectionConfig?.knex || knex(connectionConfig.connection); + this.sqlClient = + connectionConfig?.knex || knex(connectionConfig.connection); this.queries = queries; this._version = {}; } @@ -1557,7 +1558,13 @@ class SqliteClient extends KnexClient { upQuery, ); - await this.sqlClient.raw('PRAGMA foreign_keys = OFF;'); + const fkCheckEnabled = ( + await this.sqlClient.raw('PRAGMA foreign_keys;') + )?.[0]?.foreign_keys; + + if (fkCheckEnabled) + await this.sqlClient.raw('PRAGMA foreign_keys = OFF;'); + await this.sqlClient.raw('PRAGMA legacy_alter_table = ON;'); const trx = await this.sqlClient.transaction(); @@ -1594,7 +1601,8 @@ class SqliteClient extends KnexClient { log.ppe(e, _func); throw e; } finally { - await this.sqlClient.raw('PRAGMA foreign_keys = ON;'); + if (fkCheckEnabled) + await this.sqlClient.raw('PRAGMA foreign_keys = ON;'); await this.sqlClient.raw('PRAGMA legacy_alter_table = OFF;'); }