Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect display of compound primary keys with foreign key relationships #319

Closed
simonw opened this issue Jun 20, 2018 · 2 comments
Closed
Labels
Milestone

Comments

@simonw
Copy link
Owner

simonw commented Jun 20, 2018

https://registry.datasette.io/registry-7d4f81f/datasette_tags

2018-06-20 at 9 07 am

Underlying JSON looks like this:

{
    "database": "registry",
    "table": "datasette_tags",
    "is_view": false,
    "human_description_en": "",
    "rows": [
        {
            "datasette_id": {
                "value": 1,
                "label": "Global Power Plant Database"
            },
            "tag": {
                "value": "geospatial",
                "label": "geospatial"
            }
        },

Bug is likely somewhere in here:

if link_column:
cells.append(
{
"column": pks[0] if len(pks) == 1 else "Link",
"value": jinja2.Markup(
'<a href="/{database}/{table}/{flat_pks_quoted}">{flat_pks}</a>'.format(
database=database,
table=urllib.parse.quote_plus(table),
flat_pks=str(
jinja2.escape(
path_from_row_pks(row, pks, not pks, False)
)
),
flat_pks_quoted=path_from_row_pks(row, pks, not pks),
)
),
}
)
for value, column_dict in zip(row, columns):
column = column_dict["name"]
if link_column and len(pks) == 1 and column == pks[0]:
# If there's a simple primary key, don't repeat the value as it's
# already shown in the link column.
continue
if isinstance(value, dict):
# It's an expanded foreign key - display link to other row
label = value["label"]
value = value["value"]
# The table we link to depends on the column
other_table = column_to_foreign_key_table[column]
link_template = (
LINK_WITH_LABEL if (label != value) else LINK_WITH_VALUE
)
display_value = jinja2.Markup(link_template.format(
database=database,
table=urllib.parse.quote_plus(other_table),
link_id=urllib.parse.quote_plus(str(value)),
id=str(jinja2.escape(value)),
label=str(jinja2.escape(label)),
))
elif value is None:
display_value = jinja2.Markup("&nbsp;")
elif is_url(str(value).strip()):
display_value = jinja2.Markup(
'<a href="{url}">{url}</a>'.format(
url=jinja2.escape(value.strip())
)
)
elif column in table_metadata.get("units", {}) and value != "":
# Interpret units using pint
value = value * ureg(table_metadata["units"][column])
# Pint uses floating point which sometimes introduces errors in the compact
# representation, which we have to round off to avoid ugliness. In the vast
# majority of cases this rounding will be inconsequential. I hope.
value = round(value.to_compact(), 6)
display_value = jinja2.Markup(
"{:~P}".format(value).replace(" ", "&nbsp;")
)
else:
display_value = str(value)
cells.append({"column": column, "value": display_value})
cell_rows.append(cells)

@simonw simonw added the bug label Jun 20, 2018
@simonw simonw changed the title Weird display of compound primary keys with foreign key relationships Incorrect display of compound primary keys with foreign key relationships Jun 20, 2018
@simonw simonw added this to the 0.23.1 milestone Jun 20, 2018
@simonw simonw closed this as completed in 3b53eea Jun 21, 2018
@simonw
Copy link
Owner Author

simonw commented Jun 21, 2018

@simonw
Copy link
Owner Author

simonw commented Jun 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant