diff --git a/packages/backend-core/src/sql/sql.ts b/packages/backend-core/src/sql/sql.ts index e4b2b843af..4ec9246cbb 100644 --- a/packages/backend-core/src/sql/sql.ts +++ b/packages/backend-core/src/sql/sql.ts @@ -1191,8 +1191,9 @@ class InternalBuilder { return withSchema } - private buildJsonField(field: string): string { + private buildJsonField(table: Table, field: string): string { const parts = field.split(".") + const baseName = parts[parts.length - 1] let unaliased: string let tableField: string @@ -1205,10 +1206,22 @@ class InternalBuilder { tableField = unaliased } + const schema = table.schema[baseName] + const separator = this.client === SqlClient.ORACLE ? " VALUE " : "," - return this.knex - .raw(`?${separator}??`, [unaliased, this.rawQuotedIdentifier(tableField)]) - .toString() + let identifier = this.rawQuotedIdentifier(tableField) + if (schema.type === FieldType.BIGINT) { + identifier = this.castIntToString(identifier) + } else if (schema.type === FieldType.LINK) { + const otherTable = this.query.meta.tables![schema.tableId] + const otherField = otherTable.schema[schema.fieldName] + if (otherField.type === FieldType.BIGINT) { + identifier = this.castIntToString(identifier) + } + } else if (schema.autocolumn && schema.autoReason === "foreign_key") + return this.knex + .raw(`?${separator}??`, [unaliased, identifier]) + .toString() } maxFunctionParameters() { @@ -1272,7 +1285,7 @@ class InternalBuilder { Math.floor(this.maxFunctionParameters() / 2) ) const fieldList: string = relationshipFields - .map(field => this.buildJsonField(field)) + .map(field => this.buildJsonField(relatedTable!, field)) .join(",") // SQL Server uses TOP - which performs a little differently to the normal LIMIT syntax // it reduces the result set rather than limiting how much data it filters over diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index f0dc27708c..0ad5439653 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -3545,7 +3545,19 @@ if (descriptions.length) { await config.api.row.save(relatedTable._id!, { tableid: row.id }) const { rows } = await config.api.row.search(table._id!) - expect(rows).toEqual([]) + expect(rows).toEqual([ + expect.objectContaining({ + _id: "%5B'1'%5D", + _rev: "rev", + id: "1", + related: [ + { + _id: "%5B'1'%5D", + primaryDisplay: 1, + }, + ], + }), + ]) }) }) } diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 881017c24e..28400f616f 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -39,10 +39,6 @@ if (types) { types.setTypeParser(1114, (val: any) => val) // timestamp types.setTypeParser(1082, (val: any) => val) // date types.setTypeParser(1184, (val: any) => val) // timestampz - // types.setTypeParser(114, JSON.parse) // json - // types.setTypeParser(3802, JSON.parse) // jsonb - // types.setTypeParser(199, parseJsonArray) // json[] - // types.setTypeParser(3807, parseJsonArray) // jsonb[] } const JSON_REGEX = /'{\s*.*?\s*}'::json/gs