Skip to content

Commit ae2dc57

Browse files
fix: include table_comment in PostgreSQL get_table_info_sql
Use obj_description() to retrieve table comments in PostgreSQL, making table_status return 'table_comment' key like MySQL does. This fixes HTML display in Jupyter notebooks which expects the 'comment' key to be present. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bc245d3 commit ae2dc57

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/datajoint/adapters/postgres.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,12 @@ def _register_numpy_adapters(self) -> None:
151151
register_adapter(np.bool_, lambda x: AsIs(str(bool(x)).upper()))
152152

153153
# Numpy integer types
154-
for np_type in (np.int8, np.int16, np.int32, np.int64,
155-
np.uint8, np.uint16, np.uint32, np.uint64):
154+
for np_type in (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64):
156155
register_adapter(np_type, lambda x: AsIs(int(x)))
157156

158157
# Numpy float types
159-
for np_type in (np.float16, np.float32, np.float64):
160-
register_adapter(np_type, lambda x: AsIs(repr(float(x))))
158+
for np_ftype in (np.float16, np.float32, np.float64):
159+
register_adapter(np_ftype, lambda x: AsIs(repr(float(x))))
161160

162161
except ImportError:
163162
pass # numpy not available
@@ -734,11 +733,15 @@ def list_tables_sql(self, schema_name: str, pattern: str | None = None) -> str:
734733
return sql
735734

736735
def get_table_info_sql(self, schema_name: str, table_name: str) -> str:
737-
"""Query to get table metadata."""
736+
"""Query to get table metadata including table comment."""
737+
schema_str = self.quote_string(schema_name)
738+
table_str = self.quote_string(table_name)
739+
regclass_expr = f"({schema_str} || '.' || {table_str})::regclass"
738740
return (
739-
f"SELECT * FROM information_schema.tables "
740-
f"WHERE table_schema = {self.quote_string(schema_name)} "
741-
f"AND table_name = {self.quote_string(table_name)}"
741+
f"SELECT t.*, obj_description({regclass_expr}, 'pg_class') as table_comment "
742+
f"FROM information_schema.tables t "
743+
f"WHERE t.table_schema = {schema_str} "
744+
f"AND t.table_name = {table_str}"
742745
)
743746

744747
def get_columns_sql(self, schema_name: str, table_name: str) -> str:

src/datajoint/expression.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,11 +1026,7 @@ def make_sql(self, fields=None):
10261026

10271027
# PostgreSQL doesn't allow column aliases in HAVING clause (SQL standard).
10281028
# For PostgreSQL with restrictions, wrap aggregation in subquery and use WHERE.
1029-
use_subquery_for_having = (
1030-
adapter.backend == "postgresql"
1031-
and self.restriction
1032-
and self._grouping_attributes
1033-
)
1029+
use_subquery_for_having = adapter.backend == "postgresql" and self.restriction and self._grouping_attributes
10341030

10351031
if use_subquery_for_having:
10361032
# Generate inner query without HAVING
@@ -1039,9 +1035,7 @@ def make_sql(self, fields=None):
10391035
fields=fields,
10401036
from_=self.from_clause(),
10411037
where=self.where_clause(),
1042-
group_by=" GROUP BY {}".format(
1043-
", ".join(adapter.quote_identifier(col) for col in self._grouping_attributes)
1044-
),
1038+
group_by=" GROUP BY {}".format(", ".join(adapter.quote_identifier(col) for col in self._grouping_attributes)),
10451039
)
10461040
# Wrap in subquery with WHERE for the HAVING conditions
10471041
subquery_alias = adapter.quote_identifier(f"_aggr{next(self._subquery_alias_count)}")
@@ -1058,9 +1052,7 @@ def make_sql(self, fields=None):
10581052
""
10591053
if not self.primary_key
10601054
else (
1061-
" GROUP BY {}".format(
1062-
", ".join(adapter.quote_identifier(col) for col in self._grouping_attributes)
1063-
)
1055+
" GROUP BY {}".format(", ".join(adapter.quote_identifier(col) for col in self._grouping_attributes))
10641056
+ ("" if not self.restriction else " HAVING (%s)" % ")AND(".join(self.restriction))
10651057
)
10661058
),

src/datajoint/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# version bump auto managed by Github Actions:
22
# label_prs.yaml(prep), release.yaml(bump), post_release.yaml(edit)
33
# manually set this version will be eventually overwritten by the above actions
4-
__version__ = "2.1.0a1"
4+
__version__ = "2.1.0a2"

0 commit comments

Comments
 (0)