Skip to content

Commit 56a8df4

Browse files
fix: handle PostgreSQL enum types and USER-DEFINED columns
- Add udt_name to column query and use it for USER-DEFINED types - Qualify enum types with schema name in FK column definitions - PostgreSQL enums need full "schema"."enum_type" qualification Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6506bad commit 56a8df4

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/datajoint/adapters/postgres.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def get_columns_sql(self, schema_name: str, table_name: str) -> str:
718718
table_str = self.quote_string(table_name)
719719
regclass_expr = f"({schema_str} || '.' || {table_str})::regclass"
720720
return (
721-
f"SELECT c.column_name, c.data_type, c.is_nullable, c.column_default, "
721+
f"SELECT c.column_name, c.data_type, c.udt_name, c.is_nullable, c.column_default, "
722722
f"c.character_maximum_length, c.numeric_precision, c.numeric_scale, "
723723
f"col_description({regclass_expr}, c.ordinal_position) as column_comment "
724724
f"FROM information_schema.columns c "
@@ -847,9 +847,14 @@ def parse_column_info(self, row: dict[str, Any]) -> dict[str, Any]:
847847
Standardized column info with keys:
848848
name, type, nullable, default, comment, key, extra
849849
"""
850+
# For user-defined types (enums), use udt_name instead of data_type
851+
# PostgreSQL reports enums as "USER-DEFINED" in data_type
852+
data_type = row["data_type"]
853+
if data_type == "USER-DEFINED":
854+
data_type = row["udt_name"]
850855
return {
851856
"name": row["column_name"],
852-
"type": row["data_type"],
857+
"type": data_type,
853858
"nullable": row["is_nullable"] == "YES",
854859
"default": row["column_default"],
855860
"comment": row.get("column_comment"), # Retrieved via col_description()

src/datajoint/declare.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,14 @@ def compile_foreign_key(
267267

268268
# Build foreign key column definition using adapter
269269
parent_attr = ref.heading[attr]
270+
sql_type = parent_attr.sql_type
271+
# For PostgreSQL enum types, qualify with schema name
272+
# Enum type names start with "enum_" (generated hash-based names)
273+
if sql_type.startswith("enum_") and adapter.backend == "postgresql":
274+
sql_type = f"{adapter.quote_identifier(ref.database)}.{adapter.quote_identifier(sql_type)}"
270275
col_def = adapter.format_column_definition(
271276
name=attr,
272-
sql_type=parent_attr.sql_type,
277+
sql_type=sql_type,
273278
nullable=is_nullable,
274279
default=None,
275280
comment=parent_attr.sql_comment,

0 commit comments

Comments
 (0)