Skip to content

Commit 996e820

Browse files
fix: PostgreSQL compatibility for singleton tables
Add boolean_true_literal adapter property to generate correct DEFAULT value for the hidden _singleton attribute: - MySQL: DEFAULT 1 (bool maps to tinyint) - PostgreSQL: DEFAULT TRUE (native boolean) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 864b258 commit 996e820

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/datajoint/adapters/base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,21 @@ def supports_inline_indexes(self) -> bool:
563563
"""
564564
return True # Default for MySQL, override in PostgreSQL
565565

566+
@property
567+
def boolean_true_literal(self) -> str:
568+
"""
569+
Return the SQL literal for boolean TRUE.
570+
571+
MySQL uses 1 (since bool maps to tinyint).
572+
PostgreSQL uses TRUE (native boolean type).
573+
574+
Returns
575+
-------
576+
str
577+
SQL literal for boolean true value.
578+
"""
579+
return "1" # Default for MySQL, override in PostgreSQL
580+
566581
def create_index_ddl(
567582
self,
568583
full_table_name: str,

src/datajoint/adapters/postgres.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,20 @@ def supports_inline_indexes(self) -> bool:
710710
"""
711711
return False
712712

713+
@property
714+
def boolean_true_literal(self) -> str:
715+
"""
716+
Return the SQL literal for boolean TRUE.
717+
718+
PostgreSQL uses native boolean type with TRUE literal.
719+
720+
Returns
721+
-------
722+
str
723+
SQL literal for boolean true value.
724+
"""
725+
return "TRUE"
726+
713727
# =========================================================================
714728
# Introspection
715729
# =========================================================================

src/datajoint/declare.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,12 @@ def declare(
477477
primary_key = ["_singleton"]
478478
singleton_comment = ":bool:singleton primary key"
479479
sql_type = adapter.core_type_to_sql("bool")
480+
bool_literal = adapter.boolean_true_literal
480481
singleton_sql = adapter.format_column_definition(
481482
name="_singleton",
482483
sql_type=sql_type,
483484
nullable=False,
484-
default="NOT NULL DEFAULT 1",
485+
default=f"NOT NULL DEFAULT {bool_literal}",
485486
comment=singleton_comment,
486487
)
487488
attribute_sql.insert(0, singleton_sql)

0 commit comments

Comments
 (0)