Browse Source

fix: disable/enable foreign key check only if it's enabled

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5816/head
Pranav C 1 year ago
parent
commit
a5951dc2ce
  1. 14
      packages/nocodb/src/db/sql-client/lib/sqlite/SqliteClient.ts

14
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 // sqlite does not support inserting default values and knex fires a warning without this flag
connectionConfig.connection.useNullAsDefault = true; connectionConfig.connection.useNullAsDefault = true;
super(connectionConfig); super(connectionConfig);
this.sqlClient = connectionConfig?.knex || knex(connectionConfig.connection); this.sqlClient =
connectionConfig?.knex || knex(connectionConfig.connection);
this.queries = queries; this.queries = queries;
this._version = {}; this._version = {};
} }
@ -1557,7 +1558,13 @@ class SqliteClient extends KnexClient {
upQuery, 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;'); await this.sqlClient.raw('PRAGMA legacy_alter_table = ON;');
const trx = await this.sqlClient.transaction(); const trx = await this.sqlClient.transaction();
@ -1594,7 +1601,8 @@ class SqliteClient extends KnexClient {
log.ppe(e, _func); log.ppe(e, _func);
throw e; throw e;
} finally { } 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;'); await this.sqlClient.raw('PRAGMA legacy_alter_table = OFF;');
} }

Loading…
Cancel
Save