Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/datajoint/autopopulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ def __get__(self, obj, objtype=None):

jobs: Job = _JobsDescriptor()

def _declare_check(self, primary_key: list[str], fk_attribute_map: dict[str, tuple[str, str]]) -> None:
def _declare_check(
self,
primary_key: list[str],
fk_attribute_map: dict[str, tuple[str, str]],
foreign_keys: list[tuple[str, dict[str, str]]],
) -> None:
"""
Validate FK-only primary key constraint for auto-populated tables.

Expand All @@ -203,6 +208,9 @@ def _declare_check(self, primary_key: list[str], fk_attribute_map: dict[str, tup
List of primary key attribute names.
fk_attribute_map : dict
Mapping of child_attr -> (parent_table, parent_attr).
foreign_keys : list
One ``(parent_table, attr_map)`` entry per foreign key.
Not used here.

Raises
------
Expand Down
34 changes: 31 additions & 3 deletions src/datajoint/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def compile_foreign_key(
fk_attribute_map: dict[str, tuple[str, str]] | None = None,
fk_index_candidates: list[list[str]] | None = None,
column_comments: dict[str, str] | None = None,
foreign_keys: list[tuple[str, dict[str, str]]] | None = None,
) -> None:
"""
Parse a foreign key line and update declaration components.
Expand Down Expand Up @@ -239,6 +240,11 @@ def compile_foreign_key(
out of line (PostgreSQL's ``COMMENT ON COLUMN``). Each copied column's
comment, with its core-type marker (e.g. ``:int32:``), is added so the
child keeps the parent's declared type. Updated in place.
foreign_keys : list, optional
One ``(parent_table, attr_map)`` entry per foreign key, where
``attr_map`` maps each child attribute to the parent attribute it
references. Unlike ``fk_attribute_map``, it keeps every reference when
several share an attribute. Updated in place.

Raises
------
Expand Down Expand Up @@ -327,6 +333,9 @@ def compile_foreign_key(
foreign_key_sql.append(
f"FOREIGN KEY ({fk_cols}) REFERENCES {ref_table_name} ({pk_cols}) ON UPDATE CASCADE ON DELETE RESTRICT"
)
if foreign_keys is not None:
attr_map = {attr: ref.heading[attr].original_name for attr in ref.primary_key}
foreign_keys.append((ref_table_name, attr_map))

# Supporting index on the foreign-key columns.
#
Expand Down Expand Up @@ -382,7 +391,13 @@ def _tier_attributes(table_name: str | None, config) -> tuple[str, ...]:


def prepare_declare(
definition: str, context: dict, adapter, *, table_name: str | None = None, config=None
definition: str,
context: dict,
adapter,
*,
table_name: str | None = None,
config=None,
foreign_keys: list[tuple[str, dict[str, str]]] | None = None,
) -> tuple[str, list[str], list[str], list[str], list[str], list[str], dict[str, tuple[str, str]], dict[str, str]]:
"""
Parse a table definition into its components.
Expand All @@ -407,6 +422,9 @@ def prepare_declare(
declares them.
config : Config, optional
Read for ``jobs.add_job_metadata`` and ``provenance.capture``.
foreign_keys : list, optional
Receives one ``(parent_table, attr_map)`` entry per foreign
key; see :func:`compile_foreign_key`. Updated in place.

Returns
-------
Expand Down Expand Up @@ -469,6 +487,7 @@ def add_attribute(line: str, in_key: bool) -> None:
fk_attribute_map,
fk_index_candidates,
column_comments,
foreign_keys,
)
elif re.match(r"^(unique\s+)?index\s*\(.*\)\s*(#.*)?$", line, re.I): # index
compile_index(re.sub(r"\s*#.*$", "", line), index_sql, adapter)
Expand Down Expand Up @@ -534,7 +553,13 @@ def _covered(candidate: list[str]) -> bool:


def declare(
full_table_name: str, definition: str, context: dict, adapter, *, config=None
full_table_name: str,
definition: str,
context: dict,
adapter,
*,
config=None,
foreign_keys: list[tuple[str, dict[str, str]]] | None = None,
) -> tuple[str, list[str], list[str], dict[str, tuple[str, str]], list[str], list[str]]:
r"""
Parse a definition and generate SQL CREATE TABLE statement.
Expand All @@ -551,6 +576,9 @@ def declare(
Database adapter for backend-specific SQL generation.
config : Config, optional
Configuration object. If None, falls back to global config.
foreign_keys : list, optional
Receives one ``(parent_table, attr_map)`` entry per foreign
key; see :func:`compile_foreign_key`. Updated in place.

Returns
-------
Expand Down Expand Up @@ -593,7 +621,7 @@ def declare(
external_stores,
fk_attribute_map,
column_comments,
) = prepare_declare(definition, context, adapter, table_name=table_name, config=config)
) = prepare_declare(definition, context, adapter, table_name=table_name, config=config, foreign_keys=foreign_keys)

pre_ddl = [] # DDL to run BEFORE CREATE TABLE (e.g., CREATE TYPE for enums)
post_ddl = [] # DDL to run AFTER CREATE TABLE (e.g., COMMENT ON)
Expand Down
Loading