Update and delete rows
Update and delete exactly the rows you mean, know which doors refuse a call without a filter and which do not, and read why a write touched no row.
An update or a delete applies to every row that matches its filter and that the caller's policies let it see. The SDK (update(), delete()), the REST API (PATCH and DELETE /v1/db/{table}) and SQL (update, delete) all work this way; they differ in what they return and in how they treat a call without a filter.
Prerequisites#
-
A linked project:
aura link <project-id>. -
The example schema, installed with the service role key of the project:
export AURABASE_SERVICE_ROLE_KEY=aura_sk_… aura db seed --example docsIt creates
profiles,orgs,org_members,posts,todos, the viewopen_todosand the functiontodo_count(p_user uuid). See Example schema. -
AURABASE_URLandAURABASE_ANON_KEYin the environment, for the SDK andcurlexamples.
The examples create the tables qd_upd_tasks and qd_upd_notes and the view qd_upd_live_tasks, then remove them.
A table to change#
create table qd_upd_tasks (
id bigint generated always as identity primary key,
title text not null,
status text not null default 'open',
deleted_at timestamptz
);
insert into qd_upd_tasks (title) values ('Write specs'), ('Review specs'), ('Ship'), ('Celebrate');
create table qd_upd_notes (id uuid primary key default gen_random_uuid(), title text not null, body text); ℹ No results (0 rows).The table has row level security and no policy: the examples use the service_role key, on a server, except in As a signed-in user.
Update rows that match a filter#
Chain the filters before or after update(); every matching row gets the new values:
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const { data, error } = await admin.from('qd_upd_tasks').update({ status: 'done' }).ilike('title', '%specs').select('title, status')
console.log(data, error)
const none = await admin.from('qd_upd_tasks').update({ status: 'done' }).eq('title', 'Nothing like this').select('title')
console.log(none.data, none.meta?.rows_affected)[
{ title: 'Write specs', status: 'done' },
{ title: 'Review specs', status: 'done' }
] null
[] 0An update that matches no row is not an error: data is [], and meta carries rows_affected: 0 and a hint that names the two usual causes (no row matches the filter, or a policy hides the rows). The same update over REST is a PATCH with the filter in the query string:
curl -s -X PATCH "$AURABASE_URL/v1/db/qd_upd_tasks?title=eq.Ship&select=title,status" \
-H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' \
-d '{"status": "in progress"}'
echo{"data":[{"title":"Ship","status":"in progress"}],"meta":{}}And in SQL:
aura db raw "update qd_upd_tasks set status = 'blocked' where title = 'Celebrate' returning title, status" status │ title
────────┼──────────
blocked │ Celebrate
ℹ 1 row(s).update() returns the updated rows even without .select(): the gateway currently answers every PATCH with return=representation. Name the columns you need with .select('a, b') to keep the response small. The filters are those of reads: Filter operators.
Update or delete one row by id#
update(id, values) and delete(id) target the row whose id column equals the value; the REST routes are PATCH and DELETE /v1/db/{table}/{id}. They return the row as an object, and a missing id is an error, not an empty result:
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const { data: row } = await admin.from('qd_upd_tasks').select('id').eq('title', 'Ship').single()
const updated = await admin.from('qd_upd_tasks').update(String(row!.id), { status: 'done' }).select('title, status')
console.log(updated.data, updated.error)
const missing = await admin.from('qd_upd_tasks').delete('999999')
console.log(missing.error?.status, missing.error?.code){ title: 'Ship', status: 'done' } null
404 RECORD_NOT_FOUNDThese routes only work on a table whose key column is named id. For any other key, filter on it: update({ … }).eq('sku', 'LAMP-1').
Over REST, on a table whose id is a uuid:
id=$(aura db raw --json "insert into qd_upd_notes (title, body) values ('Draft', 'v1') returning id" | node -e 'console.log(JSON.parse(require("fs").readFileSync(0, "utf8"))[0].id)')
key=(-H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json')
curl -s -X PATCH "$AURABASE_URL/v1/db/qd_upd_notes/$id?select=title,body" "${key[@]}" -d '{"body": "v2"}'
echo
curl -s -X PUT "$AURABASE_URL/v1/db/qd_upd_notes/$id?select=title,body" "${key[@]}" -d '{"title": "Final", "body": "v3"}'
echo
curl -s -X DELETE "$AURABASE_URL/v1/db/qd_upd_notes/$id" "${key[@]}" -w 'DELETE %{http_code}\n'
curl -s -X DELETE "$AURABASE_URL/v1/db/qd_upd_tasks?title=eq.Nothing" "${key[@]}" -w 'DELETE with filter %{http_code}\n'{"data":{"title":"Draft","body":"v2"},"meta":{}}
{"data":{"title":"Final","body":"v3"},"meta":null}
DELETE 204
DELETE with filter 204Delete rows that match a filter#
delete() returns nothing unless you chain .select(), which returns the deleted rows:
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const quiet = await admin.from('qd_upd_tasks').delete().eq('title', 'Celebrate')
console.log(quiet.data, quiet.error)
await admin.from('qd_upd_tasks').insert({ title: 'Temporary' })
const loud = await admin.from('qd_upd_tasks').delete().eq('title', 'Temporary').select('title')
console.log(loud.data, loud.error)null null
[ { title: 'Temporary' } ] nullOver REST, a DELETE answers 204 with no body. The header Prefer: return=representation returns the deleted rows, and ?select= then chooses their columns; ?select= alone still answers 204:
aura db raw "insert into qd_upd_tasks (title) values ('Old 1'), ('Old 2')" > /dev/null
key=(-H "apikey: $AURABASE_SERVICE_ROLE_KEY")
curl -s -X DELETE "$AURABASE_URL/v1/db/qd_upd_tasks?title=eq.Old%201&select=title" "${key[@]}" -w 'select only: %{http_code}\n'
curl -s -X DELETE "$AURABASE_URL/v1/db/qd_upd_tasks?title=eq.Old%202&select=title" "${key[@]}" -H 'Prefer: return=representation' -w ' %{http_code}\n'select only: 204
{"data":[{"title":"Old 2"}],"meta":{}} 200In SQL, delete from qd_upd_tasks where title = 'Celebrate'.
Calls without a filter#
A mutation without a filter applies to every row the caller can see. Each door treats it differently:
| Door | update without a filter | delete without a filter |
|---|---|---|
| SDK | Refused before any request: UNFILTERED_MUTATION | Refused before any request: UNFILTERED_MUTATION |
REST PATCH / DELETE /v1/db/{table} | Accepted: every visible row is updated | Accepted: every visible row is deleted |
aura db raw, db.sql() | Refused: UPDATE without a WHERE clause is not allowed in raw queries | Refused: DELETE without a WHERE clause is not allowed in raw queries |
import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
const r = await admin.from('qd_upd_tasks').delete()
console.log(r.error?.code, '-', r.error?.message)UNFILTERED_MUTATION - update()/delete() without a filter rejected (anti-wipe protection): add an .eq()/... filter or use an id-based mutationThe SQL endpoint refuses both, with these exact messages:
aura db raw "update qd_upd_tasks set status = 'open'" || true
aura db raw "delete from qd_upd_tasks" || trueError: [400] BAD_REQUEST: Invalid request: UPDATE without a WHERE clause is not allowed in raw queries — risk of overwriting every row. Add a WHERE condition.
Error: [400] BAD_REQUEST: Invalid request: DELETE without a WHERE clause is not allowed in raw queries — risk of deleting every row. Add a WHERE condition.The SDK refuses on the client, so a raw HTTP call or another client gets no such protection: with the service_role key, curl -X DELETE "$AURABASE_URL/v1/db/qd_upd_tasks" empties the table, and a PATCH without a filter changes every row. Always send a filter over REST, and keep the service_role key on servers. To change every row on purpose, say so in SQL with an explicit condition, which the endpoint accepts: update qd_upd_tasks set status = 'open' where true. .or([]) and other empty filters count as no filter for the SDK. A filter on a column named like a directive of the same call (limit, offset, cursor, order, count or select, such as .eq('limit', 1).limit(1)) is refused with RESERVED_FILTER_CONFLICT.
As a signed-in user#
With the anon key and a signed-in user, the policies of the table decide which rows an update or delete can reach. todos lets each user change its own rows only: the same call that changes the user's todo touches nothing of Alice's.
import { createClient } from '@aurabase/aurabase-js'
const aura = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const email = `update-${Date.now()}@example.com`
const password = 'correct-horse-battery-9'
const signUp = await aura.auth.signUp({ email, password })
if (signUp.error) throw signUp.error
const { data: session, error } = await aura.auth.signInWithPassword({ email, password })
if (error) throw error
const me = session!.user.id
await aura.from('todos').insert({ user_id: me, title: 'Update me' })
const mine = await aura.from('todos').update({ done: true }).eq('title', 'Update me').select('title, done')
console.log('own row:', mine.data)
const alice = await aura.from('todos').update({ done: true }).eq('title', 'Create a table').select('title')
console.log('alice row:', alice.data, alice.meta?.rows_affected)
const removed = await aura.from('todos').delete().eq('title', 'Update me').select('title')
console.log('deleted:', removed.data)own row: [ { title: 'Update me', done: true } ]
alice row: [] 0
deleted: [ { title: 'Update me' } ]PostgreSQL does not raise an error for rows a policy hides: the update finds none. A policy that lets a user see a row but not change it gives the same []. The sign-up works because email confirmation is turned off on the test project; a project requires it by default.
Soft delete#
A soft delete keeps the row and marks it as deleted, so it can be restored and audited. Add a nullable deleted_at column (the table above has one), set it instead of deleting, and read through a view or a filter that hides marked rows:
create view qd_upd_live_tasks as
select id, title, status from qd_upd_tasks where deleted_at is null;
create index qd_upd_tasks_live on qd_upd_tasks (status) where deleted_at is null; ℹ No results (0 rows).import { createClient } from '@aurabase/aurabase-js'
const admin = createClient(process.env.AURABASE_URL!, process.env.AURABASE_SERVICE_ROLE_KEY!)
await admin.from('qd_upd_tasks').update({ deleted_at: new Date().toISOString() }).eq('title', 'Review specs')
const live = await admin.from('qd_upd_live_tasks').select('title').order('title')
console.log('live:', live.data)
await admin.from('qd_upd_tasks').update({ deleted_at: null }).eq('title', 'Review specs')
const restored = await admin.from('qd_upd_tasks').select('title').is('deleted_at', 'null').order('title')
console.log('restored:', restored.data)live: [ { title: 'Ship' }, { title: 'Write specs' } ]
restored: [
{ title: 'Review specs' },
{ title: 'Ship' },
{ title: 'Write specs' }
]The view runs with the caller's rights, so the policies of the table still apply to it (Views). To hide marked rows from users everywhere, put the condition in the policy itself, for example using (auth.uid() = user_id and deleted_at is null), and give the update policy a with check that allows setting deleted_at. The partial index keeps queries on live rows fast when marked rows pile up. A foreign key with on delete cascade does not fire on a soft delete: mark the child rows yourself, in the same function or trigger.
Replace a row with PUT#
PUT /v1/db/{table}/{id} replaces a whole row: columns missing from the body are set to null. The SDK has no method for it, and it is served by a different path than PATCH, with these limits measured today:
| Case | Response |
|---|---|
Key column id of type uuid, body with every not null column | 200 and the new row |
Key column of another type (such as bigint identity, like todos) | 500 INTERNAL_ERROR |
Body without a not null column | 500 INTERNAL_ERROR (the column is set to null) |
| Unknown id, or a row hidden by a policy | 404 NOT_FOUND Record '…' not found |
Use PATCH (update()) to change rows; it sends only the columns you change.
Result#
aura db raw "select title, status, deleted_at is not null as deleted from qd_upd_tasks order by title" deleted │ status │ title
────────┼────────┼─────────────
false │ done │ Review specs
false │ done │ Ship
false │ done │ Write specs
ℹ 3 row(s).Common errors#
| Message | Cause | Fix |
|---|---|---|
UNFILTERED_MUTATION update()/delete() without a filter rejected … | SDK call without a filter (status is 0: no request was sent). | Add a filter, or use update(id, …) / delete(id). |
RESERVED_FILTER_CONFLICT Filter collides with a directive … | A filter on a column named limit, offset, cursor, order, count or select, with the same directive in the chain. | Remove the directive from the mutation. |
data: [], meta.rows_affected: 0 and a hint | No row matches, or a policy hides the rows from this role. | Check the filter; check the update / delete policies. |
404 RECORD_NOT_FOUND Cannot coerce the result to a single JSON object | update(id, …) or delete(id) on an id that does not exist or is hidden. | Check the id and the policies. |
UPDATE without a WHERE clause is not allowed in raw queries … or DELETE without a WHERE clause … | aura db raw or db.sql() with an update or a delete without where. | Add a where; where true changes or deletes every row on purpose. |
403 INSUFFICIENT_PRIVILEGE new row violates row-level security policy … | The new values fail the with check of the update policy. | Change values the policy allows. |
500 INTERNAL_ERROR on PUT | Key that is not a uuid named id, or a not null column missing from the body. | Use PATCH. |
Clean up#
The test user created by the user example stays in the project; delete it with aura auth users delete <user-id>.
aura db raw "drop view if exists qd_upd_live_tasks"
aura db raw "drop table qd_upd_tasks, qd_upd_notes" ℹ No results (0 rows).
ℹ No results (0 rows).