diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 40fc2aedb4..55a896373f 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -75,9 +75,10 @@ export async function save(ctx: UserCtx) { const table = ctx.request.body const isImport = table.rows - const savedTable = await pickApi({ table }).save(ctx) + let savedTable = await pickApi({ table }).save(ctx) if (!table._id) { await events.table.created(savedTable) + savedTable = sdk.tables.enrichViewSchemas(savedTable) } else { await events.table.updated(savedTable) } diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index bb94f2bc01..34bc78b243 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -18,10 +18,11 @@ export async function save(ctx: UserCtx) { _rename?: RenameColumn } = { _id: generateTableID(), - ...rest, - type: "table", - sourceType: TableSourceType.INTERNAL, views: {}, + ...rest, + // Ensure these fields are populated, even if not sent in the request + type: rest.type || "table", + sourceType: rest.sourceType || TableSourceType.INTERNAL, } const renaming = tableToSave._rename delete tableToSave._rename diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 7fb4c0673a..b5d0107981 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -5,6 +5,7 @@ import { FieldType, INTERNAL_TABLE_SOURCE_ID, InternalTable, + NumberFieldMetadata, RelationshipType, Row, SaveTableRequest, @@ -83,7 +84,7 @@ describe("/tables", () => { }, }, views: { - view1: { + "table view": { id: "viewId", version: 2, name: "table view", @@ -96,9 +97,29 @@ describe("/tables", () => { const expected: Table = { ...tableData, type: "table", + views: { + "table view": { + ...tableData.views!["table view"], + schema: { + autoId: { + autocolumn: true, + constraints: { + presence: false, + type: "number", + }, + name: "id", + type: FieldType.NUMBER, + subtype: AutoFieldSubType.AUTO_ID, + visible: false, + } as NumberFieldMetadata, + }, + }, + }, sourceType: TableSourceType.INTERNAL, sourceId: expect.any(String), _rev: expect.stringMatching(/^1-.+/), + _id: expect.any(String), + createdAt: mocks.date.MOCK_DATE.toISOString(), updatedAt: mocks.date.MOCK_DATE.toISOString(), } expect(testTable).toEqual(expected) diff --git a/packages/server/src/sdk/app/tables/internal/index.ts b/packages/server/src/sdk/app/tables/internal/index.ts index 25fe145484..5d9feb5fe8 100644 --- a/packages/server/src/sdk/app/tables/internal/index.ts +++ b/packages/server/src/sdk/app/tables/internal/index.ts @@ -75,11 +75,13 @@ export async function save( if (!tableView) continue if (viewsSdk.isV2(tableView)) { - table.views[view] = viewsSdk.syncSchema( - oldTable!.views![view] as ViewV2, - table.schema, - renaming - ) + if (oldTable?.views && oldTable.views[view]) { + table.views[view] = viewsSdk.syncSchema( + oldTable.views[view] as ViewV2, + table.schema, + renaming + ) + } continue }