Skip to content

Commit a4ed877

Browse files
fix: convert double-quoted defaults to single quotes
PostgreSQL interprets "" as an empty identifier, not an empty string. Convert double-quoted default values (like `error_message=""`) to single quotes for PostgreSQL compatibility. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 0469a72 commit a4ed877

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/datajoint/declare.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,22 @@ def compile_attribute(
849849
match["default"] = "DEFAULT NULL" # nullable attributes default to null
850850
else:
851851
if match["default"]:
852-
quote = match["default"].split("(")[0].upper() not in CONSTANT_LITERALS and match["default"][0] not in "\"'"
853-
# Use single quotes for default values (works for both MySQL and PostgreSQL)
854-
match["default"] = "NOT NULL DEFAULT " + ("'%s'" if quote else "%s") % match["default"]
852+
default_val = match["default"]
853+
base_val = default_val.split("(")[0].upper()
854+
855+
if base_val in CONSTANT_LITERALS:
856+
# SQL constants like NULL, CURRENT_TIMESTAMP - use as-is
857+
match["default"] = f"NOT NULL DEFAULT {default_val}"
858+
elif default_val.startswith('"') and default_val.endswith('"'):
859+
# Double-quoted string - convert to single quotes for PostgreSQL
860+
inner = default_val[1:-1].replace("'", "''") # Escape single quotes
861+
match["default"] = f"NOT NULL DEFAULT '{inner}'"
862+
elif default_val.startswith("'"):
863+
# Already single-quoted - use as-is
864+
match["default"] = f"NOT NULL DEFAULT {default_val}"
865+
else:
866+
# Unquoted value - wrap in single quotes
867+
match["default"] = f"NOT NULL DEFAULT '{default_val}'"
855868
else:
856869
match["default"] = "NOT NULL"
857870

0 commit comments

Comments
 (0)