Repository navigation
Description
What
GET /v1/legal-agreements/violation-reports returns 500 for every caller:
500 {"error":"Failed to load violation reports: Could not find a relationship between 'circumvention_reports' and 'users' in the schema cache"}
/office/violations is therefore empty for everyone. Verified as both [email protected] and [email protected] (the latter holds super_admin), so it is not a permissions problem.
Cause
packages/supabase/functions/api/routes/legal-agreements.ts:45 names two foreign keys explicitly:
.from("circumvention_reports")
.select(`
*,
reported_by:users!circumvention_reports_reported_by_user_id_fkey(id, display_name, email),
organization:organizations(id, name),
worker:users!circumvention_reports_worker_user_id_fkey(id, display_name, email),
hire_agreement:hire_agreements(id, status)
`, { count: "exact" })core.circumvention_reports actually has three foreign keys:
| constraint | column | references |
|---|---|---|
circumvention_reports_hire_agreement_id_fkey |
hire_agreement_id |
core.hire_agreements |
circumvention_reports_organization_id_fkey |
organization_id |
core.organizations |
circumvention_reports_worker_user_id_fkey |
worker_user_id |
core.users |
circumvention_reports_reported_by_user_id_fkey does not exist. The column reported_by_user_id is there (uuid), and so is reviewed_by_user_id, but neither carries a foreign key. PostgREST cannot resolve the named embed, so the whole query fails — including the worker embed, which is fine.
Line 177 runs a second query against the same table; worth checking it for the same hint.
Fix
Either add the missing constraints in a new migration —
ALTER TABLE core.circumvention_reports
ADD CONSTRAINT circumvention_reports_reported_by_user_id_fkey
FOREIGN KEY (reported_by_user_id) REFERENCES core.users(id);(and the same for reviewed_by_user_id if anything wants to embed it) — or drop the reported_by embed and resolve that user in a second query. The constraint is the better answer: the column is meant to reference a user, and nothing currently enforces that it does.
Migrations are append-only here, so this is a new numbered file.
Acceptance
-
GET /v1/legal-agreements/violation-reportsreturns 200 with reports, each carryingreported_byandworker. -
/office/violationsrenders rows rather than an empty table. - An api test covers the list endpoint, so a missing embed target fails the suite instead of the screen.
- The second query at
legal-agreements.ts:177is checked for the same problem.
Activity
feat(redesign): the Office opens on what needs a decision (#839) (#902)
thinkclay commentedon Sep 25, 2026
Correction: my diagnosis above is wrong. The constraint is not missing.
I claimed
circumvention_reports_reported_by_user_id_fkey"does not exist", based on this query:That returned three rows.
information_schema.constraint_column_usagereports the referenced table and is privilege-filtered, so foreign keys pointing intoauthwere silently dropped from the result.pg_constraintshows five:The actual cause
Migration 129 created the table with its user columns split across two schemas:
The request runs against
core, and PostgREST resolves embeds only within the schemas it exposes —authis not one of them. Sousers!circumvention_reports_reported_by_user_id_fkeyhas no resolvable target and the entire select fails, taking theworkerembed down with it even though that one was always fine.Re-pointing both constraints at
core.usersfixes it.core.users.idis itselfREFERENCES auth.users(id) ON DELETE CASCADE, so the two share an id space and nothing is lost.A second bug behind the first
With the relationship resolved the query still 500s:
The embeds select
(id, display_name, email), andcore.usershas noemailcolumn — email lives inauth.users, which PostgREST does not expose even to the service role.emailwas only ever a fallback for a missing display name (row.reported_by?.display_name ?? row.reported_by?.email), sousername— which is on the table — replaces it.Also fixed in passing
The old constraints had no
ON DELETEaction, so they defaulted toNO ACTIONagainstauth.users. Deleting the account of anyone who had filed or reviewed a report would fail at that constraint — account deletion (migration 130) blocked by a violation report. Both areON DELETE SET NULLnow: the report outlives the accounts, which is what this table is for.Fix in flight; the issue title is still accurate, the "Cause" section above it is not.
fix(office): the verification queue and violation reports answer at a…
fix(office): the verification queue and violation reports answer at a…
ci(api): gate the build on HTTP api tests, via a list that may only g…