Skip to content
Snippets Groups Projects
Verified Commit f76aaacf authored by Daniel Hornung's avatar Daniel Hornung
Browse files

FIX: Foreign key specification when generating XLSX templates.

parent 44e6a553
No related branches found
No related tags found
2 merge requests!107Release v0.11.0,!102ENH: XLSX reader
......@@ -71,6 +71,11 @@ class TableTemplateGenerator(ABC):
Example: ``{"Training": {"__this__": ["date"], "Person": ["name", "email"]}}``
Here, ``date`` is the sole foreign key for Training.
| It probably is worth extending the first example, with a case where a "Training" shall
be distiguished by the "name" and "email" of a "Person" which it references. The
foreign keys for this example are specified like this:
| ``{"Training": {"__this__": [["Person", "name"], ["Person", "email"]]}}``
"""
def _generate_sheets_from_schema(self, schema: dict, foreign_keys: Optional[dict] = None
......@@ -121,7 +126,13 @@ class TableTemplateGenerator(ABC):
return sheets
def _get_foreign_keys(self, keys: dict, path: list) -> list:
"""Return the foreign keys that are needed at the location to which path points."""
"""Return the foreign keys that are needed at the location to which path points.
Returns
-------
foreign_keys: list
Contains strings or lists of strings.
"""
msg = f"A foreign key definition is missing for path:\n{path}\nKeys are:\n{keys}"
while path:
if keys is None or path[0] not in keys:
......@@ -198,14 +209,17 @@ class TableTemplateGenerator(ABC):
"string was given:\n"
f"{array_path} -> {foreigns}")
for foreign in foreigns:
internal_key = p2s(array_path + [foreign])
if isinstance(foreign, str):
foreign = [foreign]
internal_key = p2s(array_path + foreign)
if internal_key in sheets[sheetname]:
raise ValueError("The schema would lead to two columns with the same "
"name, which is forbidden:\n"
raise ValueError("The schema would lead to two columns with the "
"same name, which is forbidden:\n"
f"{foreign} -> {internal_key}")
ref_sheet = p2s(array_path)
sheets[sheetname][internal_key] = (
ColumnType.FOREIGN, f"see sheet '{ref_sheet}'", array_path + [foreign])
ColumnType.FOREIGN, f"see sheet '{ref_sheet}'",
array_path + foreign)
# Columns are added to the new sheet, thus we do not return any columns for the
# current sheet.
return {}
......@@ -301,7 +315,7 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
definition dict
You need to pass the dict of a single sheet to this function.
"""
return max([len(path) for _, _, path in sheetdef.values()])
return max(len(path) for _, _, path in sheetdef.values())
@staticmethod
def _get_ordered_cols(sheetdef: dict) -> list:
......
......@@ -263,7 +263,7 @@ def test_model_with_indirect_reference():
_compare_generated_to_known_good(
schema_file=rfp("data/indirect_schema.json"),
known_good=rfp("data/indirect_template.xlsx"),
foreign_keys={"Wrapper": ["Training.name", "Training.url"]},
foreign_keys={"Wrapper": {"__this__": [["Training", "name"], ["Training", "url"]]}},
outfile=None)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment