Update row
update
You can update rows by calling the update
function like this:
from abstra.tables import update
updated = update(
"users",
where={"email": "michael.scott@dundermiflin.com"},
set={"name": "Michael Scott"}
)
updated # [{"id": 123, "email": "michael.scott@dundermiflin.com", "name": "Michael Scott"}]
Which is equivalent to
UPDATE users
SET name = 'Michael Scott'
WHERE email = 'michael.scott@dundermiflin.com'
RETURNING *
Dates, datetimes may also be passed directly to where and set arguments:
from abstra.tables import update
updated = update(
"episodes",
where={
"aired_at": datetime.date(2013, 5, 16),
"series": "The Office",
},
set={"trivia": {"imdb_rating": 9.9}}
)
updated # [{"id": 123, "series": "The Offcie", "name": "Finale", "aired_at": "2013-05-16", {"trivia": {"imdb_rating": 9.9}}}]
update_by_id
If you only need to set the id
in the where
filter, you can use update_by_id
instead:
from abstra.tables import update_by_id
updated = update_by_id(
"users",
id=123,
values={"name": "Michael Scott"}
)
updated # [{"id": 123, "email": "michael.scott@dundermiflin.com", "name": "Michael Scott"}]
info
Update supports the same value types as insert.
Which is equivalent to
UPDATE users
SET name = 'Michael Scott'
WHERE id = 123
RETURNING *