Skip to content

Commit 01c6af4

Browse files
refactor(#1512): express FK auto-indexing as an adapter capability
Replace the `adapter.backend == "postgresql"` string check in declare.py with a new adapter capability, `auto_indexes_foreign_keys` (MySQL True, PostgreSQL False), mirroring `supports_inline_indexes`. The backend *policy* (who indexes FK columns) now lives on the adapter; the coverage/redundancy logic stays in declare.py as backend-agnostic schema-structure reasoning. Scales to new backends without touching declare.py.
1 parent a69d454 commit 01c6af4

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/datajoint/adapters/base.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,22 @@ def supports_inline_indexes(self) -> bool:
617617
"""
618618
return True # Default for MySQL, override in PostgreSQL
619619

620+
@property
621+
def auto_indexes_foreign_keys(self) -> bool:
622+
"""
623+
Whether this backend implicitly indexes a foreign key's referencing
624+
(child) columns as part of enforcing the constraint.
625+
626+
MySQL/InnoDB does; PostgreSQL does not (and offers no server setting to
627+
make it), so DataJoint must emit an explicit index on that backend.
628+
629+
Returns
630+
-------
631+
bool
632+
True for MySQL, False for PostgreSQL.
633+
"""
634+
return True # Default for MySQL, override in PostgreSQL
635+
620636
def create_index_ddl(
621637
self,
622638
full_table_name: str,

‎src/datajoint/adapters/postgres.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,14 @@ def supports_inline_indexes(self) -> bool:
720720
"""
721721
return False
722722

723+
@property
724+
def auto_indexes_foreign_keys(self) -> bool:
725+
"""
726+
PostgreSQL never indexes a foreign key's referencing columns
727+
automatically, so DataJoint emits an explicit (coverage-aware) index.
728+
"""
729+
return False
730+
723731
# =========================================================================
724732
# Introspection
725733
# =========================================================================

‎src/datajoint/declare.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def compile_foreign_key(
321321
if is_unique:
322322
index_cols = ", ".join(adapter.quote_identifier(attr) for attr in fk_attrs)
323323
index_sql.append(f"UNIQUE INDEX ({index_cols})")
324-
elif adapter.backend == "postgresql" and fk_index_candidates is not None:
324+
elif not adapter.auto_indexes_foreign_keys and fk_index_candidates is not None:
325325
fk_index_candidates.append(fk_attrs)
326326

327327

0 commit comments

Comments
 (0)