Fast HTML - 500 Server Error NotFoundError: Need 2 pk
In case anybody runs into this issue using fast HTML where they are trying to get rows from a table with multiple primary keys And get some variation of need 2 PK
or need two primary keys
Question
500 Server Error NotFoundError: Need 2 pk
My schema are defined as:
users.create(dict(username=str, pwd=str, role=str), pk='username',transform=True)
imgs.create(id=int, username=str, mime=str, b64=str, created_at=str, score=int, pk=('id', 'username'),transform=True)
...
imgs = imgs() # This Is where I'm trying to return the list of images.
Answer
To be explicit: the problem is that the table is expecting two primary keys.
And you can do so like this: imgs[['1', "admin"]
as per the mini data API Spec. But, this returns one image.
Say you wanna get all of the images by a specific user:
users.username = "admin"
imgs = imgs.rows_where("username = ?", [users.username]), None))
"Give me all the rows where the username is 'admin' (And if not found give me None
)"
Or how about getting the first image that matches a specific ID:
id = 0
img = next(imgs.rows_where("id = ?", [id]), None)
"Give me the first row where the id is 0, (And if not found give me None
)"
Where imgs
is of type <class 'sqlite_minutils.db.Table'>
. And next
returns the first item; the second argument is the default.
Summary and other details
There may be a more idiomatic fast HTML way to do this. However I do like how the expressions read nicely.
And
It's worth noting that the type of <class 'sqlite_minutils.db.Table'>
is a subclass of the type <class 'sqlite_utils.db.Table'>
. So we can check out the docs for that here https://sqlite-utils.datasette.io/en/stable/python-api.html#listing-rows
Top comments (0)