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

500 error if sorted by a column not in the ?_col= list #1773

Closed
simonw opened this issue Jul 27, 2022 · 4 comments
Closed

500 error if sorted by a column not in the ?_col= list #1773

simonw opened this issue Jul 27, 2022 · 4 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Jul 27, 2022

For example: https://latest.datasette.io/fixtures/sortable?_sort_desc=sortable&_col=sortable_with_nulls

That's ?_sort_desc=sortable&_col=sortable_with_nulls

image

@simonw simonw added the bug label Jul 27, 2022
@simonw
Copy link
Owner Author

simonw commented Jul 27, 2022

Traceback:

Traceback (most recent call last):
  File "/Users/simon/Dropbox/Development/datasette/datasette/app.py", line 1264, in route_path
    response = await view(request, send)
  File "/Users/simon/Dropbox/Development/datasette/datasette/views/base.py", line 134, in view
    return await self.dispatch_request(request)
  File "/Users/simon/Dropbox/Development/datasette/datasette/views/base.py", line 91, in dispatch_request
    return await handler(request)
  File "/Users/simon/Dropbox/Development/datasette/datasette/views/base.py", line 361, in get
    response_or_template_contexts = await self.data(request, **data_kwargs)
  File "/Users/simon/Dropbox/Development/datasette/datasette/views/table.py", line 157, in data
    return await self._data_traced(request, default_labels, _next, _size)
  File "/Users/simon/Dropbox/Development/datasette/datasette/views/table.py", line 633, in _data_traced
    prefix = rows[-2][sort or sort_desc]
IndexError: No item with that key

That's this code here:

# If there's a sort or sort_desc, add that value as a prefix
if (sort or sort_desc) and not is_view:
prefix = rows[-2][sort or sort_desc]
if isinstance(prefix, dict) and "value" in prefix:
prefix = prefix["value"]
if prefix is None:
prefix = "$null"
else:
prefix = tilde_encode(str(prefix))
next_value = f"{prefix},{next_value}"
added_args = {"_next": next_value}
if sort:
added_args["_sort"] = sort
else:
added_args["_sort_desc"] = sort_desc

@simonw
Copy link
Owner Author

simonw commented Jul 27, 2022

So the problem here is that in generating the ?_next= next page link we need the value from the specified sort column - but we're not selecting it any more.

Possible fixes:

  • Always include the sort column in the list of columns that are selected, then filter that out before they are displayed
  • Use a second query to figure out the _sort or _sort_desc value for that last row, since we know its primary key (we always select primary keys)

Not sure which solution is more elegant. I think it might be the second one.

@simonw
Copy link
Owner Author

simonw commented Jul 27, 2022

So code would look something like this:

try:
    prefix = rows[-2][sort or sort_desc]
except KeyError:
    # Didn't select sort/sort_desc column - look up value by primary key instead
    primary_key = rows[-2]["pk"] # But more complex than this
    prefix = (await db.execute("select * from {table} where pk = ?", [primary_key])).first_value()

@simonw simonw added this to the Datasette 0.62 milestone Aug 14, 2022
@simonw simonw closed this as completed in df4fd2d Aug 14, 2022
@simonw
Copy link
Owner Author

simonw commented Aug 14, 2022

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