Skip to content

Violation reports 500 on every request — the list query embeds a foreign key that was never created #901

Closed

Description

@thinkclay

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-reports returns 200 with reports, each carrying reported_by and worker.
  • /office/violations renders 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:177 is checked for the same problem.

Activity

  1. added
    agent-ready
    on Sep 25, 2026
  2. thinkclay commented on Sep 25, 2026

    @thinkclay
    ContributorAuthor

    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:

    select tc.constraint_name, kcu.column_name, ccu.table_schema||'.'||ccu.table_name
      from information_schema.table_constraints tc
      join information_schema.key_column_usage kcu on kcu.constraint_name = tc.constraint_name
      join information_schema.constraint_column_usage ccu on ccu.constraint_name = tc.constraint_name
     where tc.constraint_type = 'FOREIGN KEY' and tc.table_name = 'circumvention_reports';

    That returned three rows. information_schema.constraint_column_usage reports the referenced table and is privilege-filtered, so foreign keys pointing into auth were silently dropped from the result. pg_constraint shows five:

    circumvention_reports_hire_agreement_id_fkey   → core.hire_agreements(id) ON DELETE SET NULL
    circumvention_reports_organization_id_fkey     → core.organizations(id)   ON DELETE SET NULL
    circumvention_reports_reported_by_user_id_fkey → auth.users(id)
    circumvention_reports_reviewed_by_user_id_fkey → auth.users(id)
    circumvention_reports_worker_user_id_fkey      → core.users(id)           ON DELETE SET NULL
    

    The actual cause

    Migration 129 created the table with its user columns split across two schemas:

    reported_by_user_id UUID REFERENCES auth.users(id),
    worker_user_id      UUID REFERENCES core.users(id) ON DELETE SET NULL,
    reviewed_by_user_id UUID REFERENCES auth.users(id),

    The request runs against core, and PostgREST resolves embeds only within the schemas it exposes — auth is not one of them. So users!circumvention_reports_reported_by_user_id_fkey has no resolvable target and the entire select fails, taking the worker embed down with it even though that one was always fine.

    Re-pointing both constraints at core.users fixes it. core.users.id is itself REFERENCES 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:

    {"error":"Failed to load violation reports: column users_1.email does not exist"}
    

    The embeds select (id, display_name, email), and core.users has no email column — email lives in auth.users, which PostgREST does not expose even to the service role. email was only ever a fallback for a missing display name (row.reported_by?.display_name ?? row.reported_by?.email), so username — which is on the table — replaces it.

    Also fixed in passing

    The old constraints had no ON DELETE action, so they defaulted to NO ACTION against auth.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 are ON DELETE SET NULL now: 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-ready

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions