diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 04f9054947..3abdca693d 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -18,6 +18,7 @@ import Validator from 'validator'; import { customAlphabet } from 'nanoid'; import DOMPurify from 'isomorphic-dompurify'; import { v4 as uuidv4 } from 'uuid'; +import type { SortType } from 'nocodb-sdk'; import type { Knex } from 'knex'; import type LookupColumn from '~/models/LookupColumn'; import type { XKnex } from '~/db/CustomKnex'; @@ -35,15 +36,6 @@ import type { SelectOption, User, } from '~/models'; -import type { SortType } from 'nocodb-sdk'; -import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2'; -import genRollupSelectv2 from '~/db/genRollupSelectv2'; -import conditionV2 from '~/db/conditionV2'; -import sortV2 from '~/db/sortV2'; -import { customValidators } from '~/db/util/customValidators'; -import { extractLimitAndOffset } from '~/helpers'; -import { NcError } from '~/helpers/catchError'; -import getAst from '~/helpers/getAst'; import { Audit, BaseUser, @@ -55,6 +47,14 @@ import { Source, View, } from '~/models'; +import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2'; +import genRollupSelectv2 from '~/db/genRollupSelectv2'; +import conditionV2 from '~/db/conditionV2'; +import sortV2 from '~/db/sortV2'; +import { customValidators } from '~/db/util/customValidators'; +import { extractLimitAndOffset } from '~/helpers'; +import { NcError } from '~/helpers/catchError'; +import getAst from '~/helpers/getAst'; import { sanitize, unsanitize } from '~/helpers/sqlSanitize'; import Noco from '~/Noco'; import { HANDLE_WEBHOOK } from '~/services/hook-handler.service'; @@ -2358,7 +2358,7 @@ class BaseModelSqlv2 { await this.beforeInsert(insertObj, trx, cookie); } - await this.prepareNocoData(insertObj); + await this.prepareNocoData(insertObj, true); let response; // const driver = trx ? trx : this.dbDriver; @@ -2709,7 +2709,6 @@ class BaseModelSqlv2 { this.clientMeta, this.dbDriver, ); - let rowId = null; const nestedCols = (await this.model.getColumns()).filter((c) => @@ -2725,6 +2724,8 @@ class BaseModelSqlv2 { await this.beforeInsert(insertObj, this.dbDriver, cookie); + await this.prepareNocoData(insertObj, true); + let response; const query = this.dbDriver(this.tnPath).insert(insertObj); @@ -3068,7 +3069,7 @@ class BaseModelSqlv2 { } } - await this.prepareNocoData(insertObj); + await this.prepareNocoData(insertObj, true); // prepare nested link data for insert only if it is single record insertion if (isSingleRecordInsertion) { @@ -5632,13 +5633,28 @@ class BaseModelSqlv2 { await this.execAndParse(qb, null, { raw: true }); } - async prepareNocoData(data) { + async prepareNocoData(data, isInsertData = false) { if ( this.model.columns.some((c) => - [UITypes.Attachment, UITypes.User].includes(c.uidt), + [ + UITypes.Attachment, + UITypes.User, + UITypes.CreateTime, + UITypes.LastModifiedTime, + ].includes(c.uidt), ) ) { for (const column of this.model.columns) { + if ( + isInsertData && + column.uidt === UITypes.CreateTime && + column.system + ) { + data[column.column_name] = Noco.ncMeta.now(); + } + if (column.uidt === UITypes.LastModifiedTime && column.system) { + data[column.column_name] = Noco.ncMeta.now(); + } if (column.uidt === UITypes.Attachment) { if (data[column.column_name]) { if (Array.isArray(data[column.column_name])) { diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index 4c735a1280..bd1ff10969 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -1688,6 +1688,7 @@ export class ColumnsService { case UITypes.CreateTime: case UITypes.LastModifiedTime: { + let columnName: string; const columns = await table.getColumns(); // check if column already exists, then just create a new column in meta // else create a new column in meta and db @@ -1699,7 +1700,7 @@ export class ColumnsService { ); if (!existingColumn) { - let columnName = + columnName = colBody.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'; // const sqlClient = await reuseOrSave('sqlClient', reuse, async () => // NcConnectionMgrv2.getSqlClient(source), @@ -1718,9 +1719,14 @@ export class ColumnsService { columnName = getUniqueColumnName(columns, columnName); } - if (!dbColumn) { - // create column in db + { + // remove default value for SQLite since it doesn't support default value as function when adding column + // only support default value as constant value + if (source.type === 'sqlite3') { + colBody.cdf = null; + } + // create column in db const tableUpdateBody = { ...table, tn: table.table_name, @@ -1732,7 +1738,7 @@ export class ColumnsService { ...table.columns.map((c) => ({ ...c, cn: c.column_name })), { ...colBody, - cn: UITypes.CreateTime ? 'created_at' : 'updated_at', + cn: columnName, altered: Altered.NEW_COLUMN, }, ], @@ -1753,11 +1759,15 @@ export class ColumnsService { title, system: 1, fk_model_id: table.id, + column_name: columnName, }); + } else { + columnName = existingColumn.column_name; } await Column.insert({ ...colBody, fk_model_id: table.id, + column_name: columnName, }); } break;