diff --git a/packages/nocodb/src/lib/db/sql-client/lib/mssql/MssqlClient.ts b/packages/nocodb/src/lib/db/sql-client/lib/mssql/MssqlClient.ts index 6b76897da1..8228539423 100644 --- a/packages/nocodb/src/lib/db/sql-client/lib/mssql/MssqlClient.ts +++ b/packages/nocodb/src/lib/db/sql-client/lib/mssql/MssqlClient.ts @@ -356,13 +356,11 @@ class MssqlClient extends KnexClient { try { /** ************** START : create _evolution table if not exists *************** */ - const exists = await this.sqlClient.schema.hasTable( - this.getTnPath(args.tn) - ); + const exists = await this.sqlClient.schema.withSchema(this.schema).hasTable(args.tn); if (!exists) { - await this.sqlClient.schema.createTable( - this.getTnPath(args.tn), + await this.sqlClient.schema.withSchema(this.schema).createTable( + args.tn, function (table) { table.increments(); table.string('title').notNullable(); @@ -396,9 +394,7 @@ class MssqlClient extends KnexClient { log.api(`${_func}:args:`, args); try { - result.data.value = await this.sqlClient.schema.hasTable( - this.getTnPath(args.tn) - ); + result.data.value = await this.sqlClient.schema.withSchema(this.schema).hasTable(args.tn); } catch (e) { log.ppe(e, _func); throw e; @@ -1599,9 +1595,9 @@ class MssqlClient extends KnexClient { const result = new Result(); log.api(`${func}:args:`, args); try { - const query = `CREATE TRIGGER ${args.trigger_name} on ${this.getTnPath( + const query = `CREATE TRIGGER [${args.trigger_name}] on [${this.getTnPath( args.tn - )} \n${args.timing} ${args.event}\n as\n${args.statement}`; + )}] \n${args.timing} ${args.event}\n as\n${args.statement}`; await this.sqlClient.raw(query); result.data.object = { upStatement: [{ sql: this.querySeparator() + query }], @@ -1817,7 +1813,7 @@ class MssqlClient extends KnexClient { const downStatement = this.querySeparator() + - this.sqlClient.schema.dropTable(args.table).toString(); + this.sqlClient.schema.withSchema(this.schema).dropTable(args.table).toString(); this.emit(`Success : ${upQuery}`); @@ -1853,11 +1849,11 @@ class MssqlClient extends KnexClient { const triggerName = `xc_trigger_${args.table_name}_${column.column_name}`; const triggerCreateQuery = this.querySeparator() + - `CREATE TRIGGER ${this.schema}.${triggerName} ON ${this.schema}.${args.table_name} AFTER UPDATE + `CREATE TRIGGER [${this.schema}].[${triggerName}] ON [${this.schema}].[${args.table_name}] AFTER UPDATE AS BEGIN SET NOCOUNT ON; - UPDATE ${this.schema}.${args.table_name} Set ${column.column_name} = GetDate() where ${pk.column_name} in (SELECT ${pk.column_name} FROM Inserted) + UPDATE [${this.schema}].[${args.table_name}] Set [${column.column_name}] = GetDate() where [${pk.column_name}] in (SELECT [${pk.column_name}] FROM Inserted) END;`; upQuery += triggerCreateQuery; @@ -1888,11 +1884,11 @@ class MssqlClient extends KnexClient { const triggerName = `xc_trigger_${args.table_name}_${column.column_name}`; const triggerCreateQuery = this.querySeparator() + - `CREATE TRIGGER ${this.schema}.${triggerName} ON ${this.schema}.${args.table_name} AFTER UPDATE + `CREATE TRIGGER [${this.schema}].[${triggerName}] ON [${this.schema}].[${args.table_name}] AFTER UPDATE AS BEGIN SET NOCOUNT ON; - UPDATE ${this.schema}.${args.table_name} Set ${column.column_name} = GetDate() where ${pk.column_name} in (SELECT ${pk.column_name} FROM Inserted) + UPDATE [${this.schema}].[${args.table_name}] Set [${column.column_name}] = GetDate() where [${pk.column_name}] in (SELECT [${pk.column_name}] FROM Inserted) END;`; upQuery += triggerCreateQuery; @@ -1901,7 +1897,7 @@ class MssqlClient extends KnexClient { downQuery += this.querySeparator() + - `DROP TRIGGER IF EXISTS ${this.schema}.${triggerName};`; + `DROP TRIGGER IF EXISTS [${this.schema}].[${triggerName}];`; } } result.upStatement[0] = { sql: upQuery }; @@ -2059,13 +2055,13 @@ class MssqlClient extends KnexClient { /** ************** create up & down statements *************** */ const upStatement = this.querySeparator() + - this.sqlClient.schema.dropTable(this.getTnPath(args.tn)).toString(); + this.sqlClient.schema.withSchema(this.schema).dropTable(args.tn).toString(); let downQuery = this.querySeparator() + this.createTable(args.tn, args); this.emit(`Success : ${upStatement}`); let relationsList: any = await this.relationList({ - tn: this.getTnPath(args.tn), + tn: args.tn, }); relationsList = relationsList.data.list; @@ -2073,12 +2069,12 @@ class MssqlClient extends KnexClient { for (const relation of relationsList) { downQuery += this.querySeparator() + - (await this.sqlClient.schema - .table(this.getTnPath(relation.tn), function (table) { + (await this.sqlClient.withSchema(this.schema).schema + .table(relation.tn, (table) => { table = table .foreign(relation.cn, null) .references(relation.rcn) - .on(relation.rtn); + .on(this.getTnPath(relation.rtn)); if (relation.ur) { table = table.onUpdate(relation.ur); @@ -2117,7 +2113,7 @@ class MssqlClient extends KnexClient { )) { downQuery += this.querySeparator() + - this.sqlClient.schema + this.sqlClient.schema.withSchema(this.schema) .table(tn, function (table) { if (non_unique) { table.index(columns, key_name); @@ -2129,7 +2125,7 @@ class MssqlClient extends KnexClient { } /** ************** drop tn *************** */ - await this.sqlClient.schema.dropTable(this.getTnPath(args.tn)); + await this.sqlClient.schema.withSchema(this.schema).dropTable(args.tn); /** ************** return files *************** */ result.data.object = { @@ -2144,6 +2140,132 @@ class MssqlClient extends KnexClient { return result; } + /** + * + * @param {Object} - args + * @param {String} - args.parentTable + * @param {String} - args.parentColumn + * @param {String} - args.childColumn + * @param {String} - args.childTable + * @returns {Promise<{upStatement, downStatement}>} + */ + async relationCreate(args) { + const _func = this.relationCreate.name; + const result = new Result(); + log.api(`${_func}:args:`, args); + + const foreignKeyName = args.foreignKeyName || null; + + try { + const self = this; + await this.sqlClient.schema.table(this.getTnPath(args.childTable), function (table) { + table = table + .foreign(args.childColumn, foreignKeyName) + .references(args.parentColumn) + .on(self.getTnPath(args.parentTable)); + + if (args.onUpdate) { + table = table.onUpdate(args.onUpdate); + } + if (args.onDelete) { + table = table.onDelete(args.onDelete); + } + }); + + const upStatement = + this.querySeparator() + + (await this.sqlClient.schema + .table(this.getTnPath(args.childTable), function (table) { + table = table + .foreign(args.childColumn, foreignKeyName) + .references(args.parentColumn) + .on(self.getTnPath(args.parentTable)); + + if (args.onUpdate) { + table = table.onUpdate(args.onUpdate); + } + if (args.onDelete) { + table = table.onDelete(args.onDelete); + } + }) + .toQuery()); + + this.emit(`Success : ${upStatement}`); + + const downStatement = + this.querySeparator() + + this.sqlClient.schema + .table(this.getTnPath(args.childTable), function (table) { + table = table.dropForeign(args.childColumn, foreignKeyName); + }) + .toQuery(); + + result.data.object = { + upStatement: [{ sql: upStatement }], + downStatement: [{ sql: downStatement }], + }; + } catch (e) { + log.ppe(e, _func); + throw e; + } + + return result; + } + + /** + * + * @param {Object} - args + * @param {String} - args.parentTable + * @param {String} - args.parentColumn + * @param {String} - args.childColumn + * @param {String} - args.childTable + * @param {String} - args.foreignKeyName + * @returns {Promise<{upStatement, downStatement}>} + */ + async relationDelete(args) { + const _func = this.relationDelete.name; + const result = new Result(); + log.api(`${_func}:args:`, args); + + const foreignKeyName = args.foreignKeyName || null; + + try { + const self = this; + await this.sqlClient.schema.table(this.getTnPath(args.childTable), function (table) { + table.dropForeign(args.childColumn, foreignKeyName); + }); + + const upStatement = + this.querySeparator() + + this.sqlClient.schema + .table(this.getTnPath(args.childTable), function (table) { + table.dropForeign(args.childColumn, foreignKeyName); + }) + .toQuery(); + + const downStatement = + this.querySeparator() + + (await this.sqlClient.schema + .table(this.getTnPath(args.childTable), function (table) { + table + .foreign(args.childColumn, foreignKeyName) + .references(args.parentColumn) + .on(self.getTnPath(args.parentTable)); + }) + .toQuery()); + + result.data.object = { + upStatement: [{ sql: upStatement }], + downStatement: [{ sql: downStatement }], + }; + } catch (e) { + log.ppe(e, _func); + throw e; + } + + return result; + } + /** * * @param args diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts index b11b1f74d3..cea8bd15fc 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts @@ -390,8 +390,11 @@ class BaseModelSqlv2 { dbDriver: this.dbDriver, }); await parentTable.getColumns(); + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); - const qb = this.dbDriver(childTable.table_name); + const qb = this.dbDriver(childTn); await childModel.selectObject({ qb }); await this.applySortAndFilter({ table: childTable, where, qb, sort }); @@ -404,7 +407,7 @@ class BaseModelSqlv2 { .select(this.dbDriver.raw('? as ??', [p, GROUP_COL])) .whereIn( chilCol.column_name, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(parentCol.column_name) // .where(parentTable.primaryKey.cn, p) .where(_wherePk(parentTable.primaryKeys, p)) @@ -477,13 +480,16 @@ class BaseModelSqlv2 { const parentTable = await parentCol.getModel(); await parentTable.getColumns(); + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + const children = await this.dbDriver.unionAll( ids.map((p) => { - const query = this.dbDriver(childTable.table_name) + const query = this.dbDriver(childTn) .count(`${chilCol?.column_name} as count`) .whereIn( chilCol.column_name, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(parentCol.column_name) // .where(parentTable.primaryKey.cn, p) .where(_wherePk(parentTable.primaryKeys, p)) @@ -525,12 +531,15 @@ class BaseModelSqlv2 { }); await parentTable.getColumns(); - const qb = this.dbDriver(childTable.table_name); + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const qb = this.dbDriver(childTn); await this.applySortAndFilter({ table: childTable, where, qb, sort }); qb.whereIn( chilCol.column_name, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(parentCol.column_name) // .where(parentTable.primaryKey.cn, p) .where(_wherePk(parentTable.primaryKeys, id)) @@ -576,11 +585,14 @@ class BaseModelSqlv2 { const parentTable = await parentCol.getModel(); await parentTable.getColumns(); - const query = this.dbDriver(childTable.table_name) + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const query = this.dbDriver(childTn) .count(`${chilCol?.column_name} as count`) .whereIn( chilCol.column_name, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(parentCol.column_name) .where(_wherePk(parentTable.primaryKeys, id)) ) @@ -607,7 +619,8 @@ class BaseModelSqlv2 { // const tn = this.model.tn; // const cn = (await relColOptions.getChildColumn()).title; - const vtn = (await relColOptions.getMMModel()).table_name; + const mmTable = await relColOptions.getMMModel(); + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name; @@ -619,7 +632,11 @@ class BaseModelSqlv2 { dbDriver: this.dbDriver, model: childTable, }); - const rtn = childTable.table_name; + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = childTn; const rtnId = childTable.id; const qb = this.dbDriver(rtn).join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`); @@ -634,7 +651,7 @@ class BaseModelSqlv2 { .clone() .whereIn( `${vtn}.${vcn}`, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(cn) // .where(parentTable.primaryKey.cn, id) .where(_wherePk(parentTable.primaryKeys, id)) @@ -680,7 +697,8 @@ class BaseModelSqlv2 { // const tn = this.model.tn; // const cn = (await relColOptions.getChildColumn()).title; - const vtn = (await relColOptions.getMMModel()).table_name; + const mmTable = await relColOptions.getMMModel(); + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name; @@ -692,14 +710,18 @@ class BaseModelSqlv2 { dbDriver: this.dbDriver, model: childTable, }); - const rtn = childTable.table_name; + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = childTn; const rtnId = childTable.id; const qb = this.dbDriver(rtn) .join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`) .whereIn( `${vtn}.${vcn}`, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(cn) // .where(parentTable.primaryKey.cn, id) .where(_wherePk(parentTable.primaryKeys, parentId)) @@ -731,16 +753,21 @@ class BaseModelSqlv2 { const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; - const vtn = (await relColOptions.getMMModel()).table_name; + const mmTable = await relColOptions.getMMModel(); + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name; const cn = (await relColOptions.getChildColumn()).column_name; const childTable = await (await relColOptions.getParentColumn()).getModel(); - const rtn = childTable.table_name; const parentTable = await (await relColOptions.getChildColumn()).getModel(); await parentTable.getColumns(); + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = childTn; + const qb = this.dbDriver(rtn) .join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`) // .select({ @@ -755,7 +782,7 @@ class BaseModelSqlv2 { .clone() .whereIn( `${vtn}.${vcn}`, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(cn) // .where(parentTable.primaryKey.cn, id) .where(_wherePk(parentTable.primaryKeys, id)) @@ -778,16 +805,21 @@ class BaseModelSqlv2 { const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; - const vtn = (await relColOptions.getMMModel()).table_name; + const mmTable = await relColOptions.getMMModel(); + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name; const cn = (await relColOptions.getChildColumn()).column_name; const childTable = await (await relColOptions.getParentColumn()).getModel(); - const rtn = childTable.table_name; const parentTable = await (await relColOptions.getChildColumn()).getModel(); await parentTable.getColumns(); + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = childTn; + const qb = this.dbDriver(rtn) .join(vtn, `${vtn}.${vrcn}`, `${rtn}.${rcn}`) // .select({ @@ -796,7 +828,7 @@ class BaseModelSqlv2 { .count(`${vtn}.${vcn}`, { as: 'count' }) .whereIn( `${vtn}.${vcn}`, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(cn) // .where(parentTable.primaryKey.cn, id) .where(_wherePk(parentTable.primaryKeys, parentId)) @@ -820,7 +852,8 @@ class BaseModelSqlv2 { const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; - const vtn = (await relColOptions.getMMModel()).table_name; + const mmTable = await relColOptions.getMMModel(); + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name; @@ -828,7 +861,11 @@ class BaseModelSqlv2 { const childTable = await (await relColOptions.getParentColumn()).getModel(); const parentTable = await (await relColOptions.getChildColumn()).getModel(); await parentTable.getColumns(); - const rtn = childTable.table_name; + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = childTn; const qb = this.dbDriver(rtn) .count(`*`, { as: 'count' }) .where((qb) => { @@ -839,7 +876,7 @@ class BaseModelSqlv2 { .join(vtn, `${rtn}.${rcn}`, `${vtn}.${vrcn}`) .whereIn( `${vtn}.${vcn}`, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(cn) // .where(parentTable.primaryKey.cn, pid) .where(_wherePk(parentTable.primaryKeys, pid)) @@ -866,7 +903,8 @@ class BaseModelSqlv2 { const relColOptions = (await relColumn.getColOptions()) as LinkToAnotherRecordColumn; - const vtn = (await relColOptions.getMMModel()).table_name; + const mmTable = await relColOptions.getMMModel(); + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; const rcn = (await relColOptions.getParentColumn()).column_name; @@ -878,7 +916,11 @@ class BaseModelSqlv2 { }); const parentTable = await (await relColOptions.getChildColumn()).getModel(); await parentTable.getColumns(); - const rtn = childTable.table_name; + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = childTn; const qb = this.dbDriver(rtn).where((qb) => qb @@ -889,7 +931,7 @@ class BaseModelSqlv2 { .join(vtn, `${rtn}.${rcn}`, `${vtn}.${vrcn}`) .whereIn( `${vtn}.${vcn}`, - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(cn) // .where(parentTable.primaryKey.cn, pid) .where(_wherePk(parentTable.primaryKeys, pid)) @@ -945,8 +987,12 @@ class BaseModelSqlv2 { const parentTable = await ( await relColOptions.getParentColumn() ).getModel(); - const tn = childTable.table_name; - const rtn = parentTable.table_name; + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const tn = childTn; + const rtn = parentTn; await parentTable.getColumns(); const qb = this.dbDriver(tn) @@ -993,8 +1039,11 @@ class BaseModelSqlv2 { }); await parentTable.getColumns(); - const tn = childTable.table_name; - const rtn = parentTable.table_name; + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const tn = childTn; + const rtn = parentTn; const qb = this.dbDriver(tn).where((qb) => { qb.whereNotIn( @@ -1054,8 +1103,11 @@ class BaseModelSqlv2 { const cn = (await relColOptions.getChildColumn()).column_name; const childTable = await (await relColOptions.getChildColumn()).getModel(); - const rtn = parentTable.table_name; - const tn = childTable.table_name; + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = parentTn; + const tn = childTn; await childTable.getColumns(); const qb = this.dbDriver(rtn) @@ -1100,8 +1152,12 @@ class BaseModelSqlv2 { dbDriver: this.dbDriver, model: parentTable, }); - const rtn = parentTable.table_name; - const tn = childTable.table_name; + + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + + const rtn = parentTn; + const tn = childTn; await childTable.getColumns(); const qb = this.dbDriver(rtn).where((qb) => { @@ -1569,15 +1625,19 @@ class BaseModelSqlv2 { return _wherePk(this.model.primaryKeys, id); } - public get tnPath() { + private getTnPath(tb: Model) { const schema = (this.dbDriver as any).searchPath?.(); const table = - this.isMssql && schema - ? this.dbDriver.raw('??.??', [schema, this.model.table_name]) - : this.model.table_name; + this.isMssql && schema + ? this.dbDriver.raw('??.??', [schema, tb.table_name]) + : tb.table_name; return table; } + public get tnPath() { + return this.getTnPath(this.model); + } + get isSqlite() { return this.clientType === 'sqlite3'; } @@ -2230,6 +2290,9 @@ class BaseModelSqlv2 { await childTable.getColumns(); await parentTable.getColumns(); + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + switch (colOptions.type) { case RelationTypes.MANY_TO_MANY: { @@ -2237,12 +2300,14 @@ class BaseModelSqlv2 { const vParentCol = await colOptions.getMMParentColumn(); const vTable = await colOptions.getMMModel(); - await this.dbDriver(vTable.table_name).insert({ - [vParentCol.column_name]: this.dbDriver(parentTable.table_name) + const vTn = this.getTnPath(vTable); + + await this.dbDriver(vTn).insert({ + [vParentCol.column_name]: this.dbDriver(parentTn) .select(parentColumn.column_name) .where(_wherePk(parentTable.primaryKeys, childId)) .first(), - [vChildCol.column_name]: this.dbDriver(childTable.table_name) + [vChildCol.column_name]: this.dbDriver(childTn) .select(childColumn.column_name) .where(_wherePk(childTable.primaryKeys, rowId)) .first(), @@ -2251,10 +2316,10 @@ class BaseModelSqlv2 { break; case RelationTypes.HAS_MANY: { - await this.dbDriver(childTable.table_name) + await this.dbDriver(childTn) .update({ [childColumn.column_name]: this.dbDriver.from( - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(parentColumn.column_name) .where(_wherePk(parentTable.primaryKeys, rowId)) .first() @@ -2266,10 +2331,10 @@ class BaseModelSqlv2 { break; case RelationTypes.BELONGS_TO: { - await this.dbDriver(childTable.table_name) + await this.dbDriver(childTn) .update({ [childColumn.column_name]: this.dbDriver.from( - this.dbDriver(parentTable.table_name) + this.dbDriver(parentTn) .select(parentColumn.column_name) .where(_wherePk(parentTable.primaryKeys, childId)) .first() @@ -2325,6 +2390,9 @@ class BaseModelSqlv2 { await childTable.getColumns(); await parentTable.getColumns(); + const childTn = this.getTnPath(childTable); + const parentTn = this.getTnPath(parentTable); + switch (colOptions.type) { case RelationTypes.MANY_TO_MANY: { @@ -2332,13 +2400,15 @@ class BaseModelSqlv2 { const vParentCol = await colOptions.getMMParentColumn(); const vTable = await colOptions.getMMModel(); - await this.dbDriver(vTable.table_name) + const vTn = this.getTnPath(vTable); + + await this.dbDriver(vTn) .where({ - [vParentCol.column_name]: this.dbDriver(parentTable.table_name) + [vParentCol.column_name]: this.dbDriver(parentTn) .select(parentColumn.column_name) .where(_wherePk(parentTable.primaryKeys, childId)) .first(), - [vChildCol.column_name]: this.dbDriver(childTable.table_name) + [vChildCol.column_name]: this.dbDriver(childTn) .select(childColumn.column_name) .where(_wherePk(childTable.primaryKeys, rowId)) .first(), @@ -2348,7 +2418,7 @@ class BaseModelSqlv2 { break; case RelationTypes.HAS_MANY: { - await this.dbDriver(childTable.table_name) + await this.dbDriver(childTn) // .where({ // [childColumn.cn]: this.dbDriver(parentTable.tn) // .select(parentColumn.cn) @@ -2361,7 +2431,7 @@ class BaseModelSqlv2 { break; case RelationTypes.BELONGS_TO: { - await this.dbDriver(childTable.table_name) + await this.dbDriver(childTn) // .where({ // [childColumn.cn]: this.dbDriver(parentTable.tn) // .select(parentColumn.cn)