Skip to content

Commit 97db517

Browse files
fix: escape single quotes in PostgreSQL COMMENT statements
Single quotes in table and column comments need to be doubled for PostgreSQL string literal syntax. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e56a5a6 commit 97db517

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/datajoint/adapters/postgres.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,9 @@ def table_comment_ddl(self, full_table_name: str, comment: str) -> str | None:
989989
>>> adapter.table_comment_ddl('"schema"."table"', 'test comment')
990990
'COMMENT ON TABLE "schema"."table" IS \\'test comment\\''
991991
"""
992-
return f"COMMENT ON TABLE {full_table_name} IS '{comment}'"
992+
# Escape single quotes by doubling them
993+
escaped_comment = comment.replace("'", "''")
994+
return f"COMMENT ON TABLE {full_table_name} IS '{escaped_comment}'"
993995

994996
def column_comment_ddl(self, full_table_name: str, column_name: str, comment: str) -> str | None:
995997
"""
@@ -1001,7 +1003,9 @@ def column_comment_ddl(self, full_table_name: str, column_name: str, comment: st
10011003
'COMMENT ON COLUMN "schema"."table"."column" IS \\'test comment\\''
10021004
"""
10031005
quoted_col = self.quote_identifier(column_name)
1004-
return f"COMMENT ON COLUMN {full_table_name}.{quoted_col} IS '{comment}'"
1006+
# Escape single quotes by doubling them (PostgreSQL string literal syntax)
1007+
escaped_comment = comment.replace("'", "''")
1008+
return f"COMMENT ON COLUMN {full_table_name}.{quoted_col} IS '{escaped_comment}'"
10051009

10061010
def enum_type_ddl(self, type_name: str, values: list[str]) -> str | None:
10071011
"""

0 commit comments

Comments
 (0)