|
|
|
@ -419,8 +419,11 @@ class BaseModelSqlv2 {
|
|
|
|
|
// if autogenerated string sort by created_at column if present
|
|
|
|
|
if (this.model.primaryKey && this.model.primaryKey.ai) { |
|
|
|
|
qb.orderBy(this.model.primaryKey.column_name); |
|
|
|
|
} else if (this.model.columns.find((c) => c.column_name === 'created_at')) { |
|
|
|
|
qb.orderBy('created_at'); |
|
|
|
|
} else { |
|
|
|
|
const createdCol = this.model.columns.find( |
|
|
|
|
(c) => c.uidt === UITypes.CreatedTime && c.system, |
|
|
|
|
); |
|
|
|
|
if (createdCol) qb.orderBy(createdCol.column_name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!ignorePagination) applyPaginate(qb, rest); |
|
|
|
@ -5149,7 +5152,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
// validate Ids
|
|
|
|
|
{ |
|
|
|
|
const childRowsQb = this.dbDriver(parentTn) |
|
|
|
|
.select(parentColumn.column_name) |
|
|
|
|
.select(`${parentTable.table_name}.${parentColumn.column_name}`) |
|
|
|
|
.select(`${vTable.table_name}.${vChildCol.column_name}`) |
|
|
|
|
.leftJoin(vTn, (qb) => { |
|
|
|
|
qb.on( |
|
|
|
@ -5162,7 +5165,6 @@ class BaseModelSqlv2 {
|
|
|
|
|
]), |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (parentTable.primaryKeys.length > 1) { |
|
|
|
|
childRowsQb.where((qb) => { |
|
|
|
|
for (const childId of childIds) { |
|
|
|
@ -5171,7 +5173,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
childRowsQb.whereIn( |
|
|
|
|
parentTable.primaryKey.column_name, |
|
|
|
|
`${parentTable.table_name}.${parentTable.primaryKey.column_name}`, |
|
|
|
|
typeof childIds[0] === 'object' |
|
|
|
|
? childIds.map( |
|
|
|
|
(c) => |
|
|
|
@ -5183,7 +5185,9 @@ class BaseModelSqlv2 {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (parentTable.primaryKey.column_name !== parentColumn.column_name) |
|
|
|
|
childRowsQb.select(parentTable.primaryKey.column_name); |
|
|
|
|
childRowsQb.select( |
|
|
|
|
`${parentTable.table_name}.${parentTable.primaryKey.column_name}`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const childRows = await this.execAndParse(childRowsQb, null, { |
|
|
|
|
raw: true, |
|
|
|
@ -5441,7 +5445,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
}); |
|
|
|
|
} else if (typeof childIds[0] === 'object') { |
|
|
|
|
childRowsQb.whereIn( |
|
|
|
|
parentTable.primaryKey.column_name, |
|
|
|
|
`${parentTable.table_name}.${parentTable.primaryKey.column_name}`, |
|
|
|
|
childIds.map( |
|
|
|
|
(c) => |
|
|
|
|
c[parentTable.primaryKey.title] || |
|
|
|
@ -5449,11 +5453,16 @@ class BaseModelSqlv2 {
|
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} else { |
|
|
|
|
childRowsQb.whereIn(parentTable.primaryKey.column_name, childIds); |
|
|
|
|
childRowsQb.whereIn( |
|
|
|
|
`${parentTable.table_name}.${parentTable.primaryKey.column_name}`, |
|
|
|
|
childIds, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (parentTable.primaryKey.column_name !== parentColumn.column_name) |
|
|
|
|
childRowsQb.select(parentTable.primaryKey.column_name); |
|
|
|
|
childRowsQb.select( |
|
|
|
|
`${parentTable.table_name}.${parentTable.primaryKey.column_name}`, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const childRows = await this.execAndParse(childRowsQb, null, { |
|
|
|
|
raw: true, |
|
|
|
@ -5492,7 +5501,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
.delete(); |
|
|
|
|
|
|
|
|
|
delQb.whereIn( |
|
|
|
|
vParentCol.column_name, |
|
|
|
|
`${vTable.table_name}.${vParentCol.column_name}`, |
|
|
|
|
typeof childIds[0] === 'object' |
|
|
|
|
? childIds.map( |
|
|
|
|
(c) => |
|
|
|
@ -5774,7 +5783,7 @@ class BaseModelSqlv2 {
|
|
|
|
|
const qb = knex(this.getTnPath(model.table_name)).update(updateObject); |
|
|
|
|
|
|
|
|
|
for (const rowId of Array.isArray(rowIds) ? rowIds : [rowIds]) { |
|
|
|
|
qb.orWhere(await this._wherePk(rowId)); |
|
|
|
|
qb.orWhere(_wherePk(model.primaryKeys, rowId)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await this.execAndParse(qb, null, { raw: true }); |
|
|
|
|