Browse Source

fix: handle ? in column name when inserting & updating

pull/2424/head
Wing-Kam Wong 2 years ago
parent
commit
cb5e4f30ff
  1. 16
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts
  2. 7
      packages/nocodb/src/lib/models/Model.ts

16
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -1242,7 +1242,7 @@ class BaseModelSqlv2 {
await populatePk(this.model, data);
// todo: filter based on view
const insertObj = await this.model.mapAliasToColumn(data, sanitize);
const insertObj = await this.model.mapAliasToColumn(data);
await this.validate(insertObj);
@ -1275,11 +1275,12 @@ class BaseModelSqlv2 {
if (response?.length) {
id = response[0];
} else {
id = (await query)[0];
id = (
await this.dbDriver.raw(query.toString().replaceAll('\\?', '?'))
)[0].insertId;
}
if (ai) {
// response = await this.readByPk(id)
response = await this.readByPk(id);
} else {
response = data;
@ -1326,14 +1327,11 @@ class BaseModelSqlv2 {
await this.beforeUpdate(data, trx, cookie);
// const driver = trx ? trx : this.dbDriver;
//
// this.validate(data);
// await this._run(
await this.dbDriver(this.tnPath)
const query = this.dbDriver(this.tnPath)
.update(updateObj)
.where(await this._wherePk(id));
// );
await this.dbDriver.raw(query.toString().replaceAll('\\?', '?'));
const response = await this.readByPk(id);
await this.afterUpdate(response, trx, cookie);

7
packages/nocodb/src/lib/models/Model.ts

@ -399,13 +399,12 @@ export default class Model implements TableType {
return true;
}
async mapAliasToColumn(data, sanitize = v => v) {
async mapAliasToColumn(data) {
const insertObj = {};
for (const col of await this.getColumns()) {
if (isVirtualCol(col)) continue;
const val =
data?.[sanitize(col.column_name)] ?? data?.[sanitize(col.title)];
if (val !== undefined) insertObj[sanitize(col.column_name)] = val;
const val = data?.[col.column_name] ?? data?.[col.title];
insertObj[col.column_name.replaceAll('?', '\\\\?')] = val;
}
return insertObj;
}

Loading…
Cancel
Save