Fast HTML – **500 Server Error NotFoundError: Need 2 pk**

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…


This content originally appeared on DEV Community and was authored by Nole Stock

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


This content originally appeared on DEV Community and was authored by Nole Stock


Print Share Comment Cite Upload Translate Updates
APA

Nole Stock | Sciencx (2024-09-04T18:59:15+00:00) Fast HTML – **500 Server Error NotFoundError: Need 2 pk**. Retrieved from https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/

MLA
" » Fast HTML – **500 Server Error NotFoundError: Need 2 pk**." Nole Stock | Sciencx - Wednesday September 4, 2024, https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/
HARVARD
Nole Stock | Sciencx Wednesday September 4, 2024 » Fast HTML – **500 Server Error NotFoundError: Need 2 pk**., viewed ,<https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/>
VANCOUVER
Nole Stock | Sciencx - » Fast HTML – **500 Server Error NotFoundError: Need 2 pk**. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/
CHICAGO
" » Fast HTML – **500 Server Error NotFoundError: Need 2 pk**." Nole Stock | Sciencx - Accessed . https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/
IEEE
" » Fast HTML – **500 Server Error NotFoundError: Need 2 pk**." Nole Stock | Sciencx [Online]. Available: https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/. [Accessed: ]
rf:citation
» Fast HTML – **500 Server Error NotFoundError: Need 2 pk** | Nole Stock | Sciencx | https://www.scien.cx/2024/09/04/fast-html-500-server-error-notfounderror-need-2-pk/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.