By Mwewa David via Discussions on Python.org at 13Apr2022 21:07:
Before updating, I am running a query to get the town i want to update
using an id. Now my assumptions are if the id does not exist I am
supposed to have an error, but I am getting none.
Can you post an example of town_dict
for a town which does not exist,
and what you get back as town
as the query result?
It is also important to say which web framekwork you’re using and which
database library. There are many choices for these
That looks like an SLQAlchemy core query to me. As such, it is not
asking “which town has this id?” Instead, it is asking “which towns
have this id?” and you will always get back a list of matches.
I would neot expect a NoResultFound
found exception, because it is not
a query which has to match something.
If the id does not exist that list would be empty; that is not an error.
If the id does exist, the list will have a row for every town with
that id. It is likely that the id is the primary key for your Town
relation. If so, the list will either have a length of 0 (no matches) or
1 (exactly one match).
So you need to:
Drop the try/except; as you have observed, it does not fire.
Change the query to look more like this:
towns = session.query(Town).where(Town.id == town_dict["id"] )
print(towns)
if not towns: # like len(towns)==0, but nicer to read
return "not results found", 404
town, = towns # nifty assignment forcing a single match
... do stuff with the town ...
return "ok" # not "ok", 200 ???
Cheers,
Cameron Simpson cs@cskk.id.au