diff --git a/src/lib/PostgresMetaColumns.ts b/src/lib/PostgresMetaColumns.ts index 6626efa7..15e56507 100644 --- a/src/lib/PostgresMetaColumns.ts +++ b/src/lib/PostgresMetaColumns.ts @@ -419,6 +419,12 @@ COMMIT;` } } +// TODO: make this more robust - use type_id or type_schema + type_name instead +// of just type. const typeIdent = (type: string) => { - return type.endsWith('[]') ? `${ident(type.slice(0, -2))}[]` : ident(type) + return type.endsWith('[]') + ? `${ident(type.slice(0, -2))}[]` + : type.includes('.') + ? type + : ident(type) } diff --git a/test/lib/columns.ts b/test/lib/columns.ts index f16402c7..85767300 100644 --- a/test/lib/columns.ts +++ b/test/lib/columns.ts @@ -947,3 +947,44 @@ test('dropping column checks', async () => { await pgMeta.query(`drop table t`) }) + +test('column with fully-qualified type', async () => { + await pgMeta.query(`create table public.t(); create schema s; create type s.my_type as enum ();`) + + const table = await pgMeta.tables.retrieve({ + schema: 'public', + name: 't', + }) + const column = await pgMeta.columns.create({ + table_id: table.data!.id, + name: 'c', + type: 's.my_type', + }) + expect(column).toMatchInlineSnapshot(` + { + "data": { + "check": null, + "comment": null, + "data_type": "USER-DEFINED", + "default_value": null, + "enums": [], + "format": "my_type", + "id": "16619.1", + "identity_generation": null, + "is_generated": false, + "is_identity": false, + "is_nullable": true, + "is_unique": false, + "is_updatable": true, + "name": "c", + "ordinal_position": 1, + "schema": "public", + "table": "t", + "table_id": 16619, + }, + "error": null, + } + `) + + await pgMeta.query(`drop table public.t; drop schema s cascade;`) +})