diff --git a/packages/server/scripts/integrations/postgres/init.sql b/packages/server/scripts/integrations/postgres/init.sql index b7ce1b7d5b..2d51a2b5cb 100644 --- a/packages/server/scripts/integrations/postgres/init.sql +++ b/packages/server/scripts/integrations/postgres/init.sql @@ -2,6 +2,10 @@ SELECT 'CREATE DATABASE main' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec CREATE SCHEMA "test-1"; CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer', 'support'); +CREATE TABLE "Bad.Table" ( + BadID SERIAL PRIMARY KEY, + "Bad.Column" text +); CREATE TABLE Persons ( PersonID SERIAL PRIMARY KEY, LastName varchar(255), diff --git a/packages/server/src/api/controllers/row/alias.ts b/packages/server/src/api/controllers/row/alias.ts index 9d54bbff8e..cb4d4f1453 100644 --- a/packages/server/src/api/controllers/row/alias.ts +++ b/packages/server/src/api/controllers/row/alias.ts @@ -55,11 +55,10 @@ export default class AliasTables { aliasField(field: string) { const tableNames = this.tableNames - if (field.includes(".")) { - const [tableName, column] = field.split(".") - if (tableNames.includes(tableName)) { - return `${this.getAlias(tableName)}.${column}` - } + const foundTable = tableNames.find(name => field.includes(name)) + if (foundTable) { + const aliasedTable = this.getAlias(foundTable) + return field.replace(foundTable, aliasedTable) } return field } diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index 61dbc11840..acf0f9898f 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -174,4 +174,13 @@ describe("Captures of real examples", () => { expect(alias).toEqual("cv") }) }) + + describe("check some edge cases", () => { + it("should handle table names/columns with dots in them", () => { + const tableNames = ["hello.world", "foo.bar.baz"] + const aliasing = new AliasTables(tableNames) + const aliased = aliasing.aliasField("hello.world.field") + expect(aliased).toEqual("a.field") + }) + }) })