diff --git a/packages/server/src/api/controllers/row/alias.ts b/packages/server/src/api/controllers/row/alias.ts index ec96bb4603..d2d41e085f 100644 --- a/packages/server/src/api/controllers/row/alias.ts +++ b/packages/server/src/api/controllers/row/alias.ts @@ -55,11 +55,16 @@ export default class AliasTables { aliasField(field: string) { const tableNames = this.tableNames - const possibleTableName = field.substring(0, field.lastIndexOf(".")) - const foundTable = tableNames.find(name => possibleTableName.includes(name)) - if (foundTable) { - const aliasedTable = this.getAlias(foundTable) - field = field.replace(foundTable, aliasedTable) + if (field.includes(".")) { + const [tableName, column] = field.split(".") + const foundTableName = tableNames.find(name => tableName.includes(name)) + if (foundTableName) { + const aliasedTableName = tableName.replace( + foundTableName, + this.getAlias(foundTableName) + ) + return `${aliasedTableName}.${column}` + } } return field } diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index 6812d358b7..3fb90a3b4d 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -176,25 +176,18 @@ describe("Captures of real examples", () => { }) describe("check some edge cases", () => { - const dotTableNames = ["hello.world", "foo.bar.baz"] + const tableNames = ["hello", "world"] - it("should handle table names/columns with dots in them", () => { - const aliasing = new AliasTables(dotTableNames) - const aliased = aliasing.aliasField(`"hello.world"."field"`) + it("should quoted table names", () => { + const aliasing = new AliasTables(tableNames) + const aliased = aliasing.aliasField(`"hello"."field"`) expect(aliased).toEqual(`"a"."field"`) }) - it("should confirm table with dots in them works with grave accents", () => { - const aliasing = new AliasTables(dotTableNames) - const aliased = aliasing.aliasField("`hello.world`.`field`") - expect(aliased).toEqual("`a`.`field`") - }) - - it("should handle if a table name is used in a column", () => { - const tableNames = ["hello", "world"] + it("should handle quoted table names with graves", () => { const aliasing = new AliasTables(tableNames) - const aliased = aliasing.aliasField(`"hello"."world_relation"`) - expect(aliased).toEqual(`"a"."world_relation"`) + const aliased = aliasing.aliasField("`hello`.`world`") + expect(aliased).toEqual("`a`.`world`") }) }) })