Skip to content

fix(postgres): FK navigation for read-only roles and uuid keys - #365

Open
Vinsho wants to merge 2 commits into
Maxteabag:mainfrom
Vinsho:fix/postgres-fk-readonly-roles
Open

Vinsho wants to merge 2 commits into
Maxteabag:mainfrom
Vinsho:fix/postgres-fk-readonly-roles

Conversation

@Vinsho

@Vinsho Vinsho commented Dec 1, 2026 •

Copy link
Copy Markdown

Problem

FK navigation (o / O in results) silently does nothing on PostgreSQL when connected as a role that only has SELECT on the tables (typical for read-only prod/stage users).

PostgresBaseAdapter.get_foreign_keys / get_referencing_foreign_keys read from information_schema.table_constraints and referential_constraints. Per the PostgreSQL docs, those views only list constraints on tables the current role owns or holds a privilege other than SELECT on. A read-only role therefore gets an empty list, so no FK metadata is attached to the results and neither key has anything to act on.

Fix

PostgreSQLAdapter (and SupabaseAdapter, which inherits it) now reads FKs from pg_catalog.pg_constraint. The catalog is visible to every role. It uses unnest(conkey, confkey) WITH ORDINALITY so composite keys keep their column order. Partition child constraints are skipped with conparentid = 0, so a partitioned table's FKs appear once.

CockroachDB and Redshift keep their existing queries, because their catalog support differs.

Second fix: jumping through uuid keys

With FKs now visible, jumping through a uuid key failed:

SELECT * FROM "currency"."currencies_data" WHERE "id" = convert_from(decode('6634…', 'hex'), 'UTF8')
-- operator does not exist: uuid = text

quote_literal produced convert_from(...), an expression typed text. It now produces a dollar-quoted literal ($sqlit$…$sqlit$). That is still independent of standard_conforming_strings, but it is untyped, so PostgreSQL coerces it to the column type (uuid, int, date, …). If the value contains the tag, the tag is extended ($sqlit1$, …). Checked on PostgreSQL: values with uuid, int, backslash+quote and an embedded tag all compare correctly.

Verification

  • Real database, as a temporary NOLOGIN role with only USAGE + SELECT on the schema, inside a rolled-back transaction:
    • old information_schema query: 0 referencing FKs
    • new pg_catalog query: 3 referencing FKs, 2 outgoing (all correct)
  • Headless app run: O on a referenced id column opens the picker and runs the lookup query.
  • Added tests/unit/test_postgres_foreign_keys_catalog.py. The full unit and UI suites pass (1805 passed, 3 skipped).

🤖 Generated with Claude Code

Vinsho and others added 2 commits December 1, 2026 14:44
information_schema.table_constraints and referential_constraints only list
constraints on tables the current role owns or holds a non-SELECT privilege
on. Read-only users therefore saw no foreign keys, and FK navigation
(o / O in results) silently did nothing.

Query pg_constraint directly, which is visible to every role. Partition
child constraints are skipped via conparentid = 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
quote_literal rendered strings as convert_from(decode(<hex>)), which is
typed text. Jumping through a uuid (or other non-text) key then failed with
"operator does not exist: uuid = text".

Emit a dollar-quoted literal instead. It is still independent of
standard_conforming_strings / backslash settings, but stays untyped so
PostgreSQL coerces it to the column type. The tag is extended when the
value would collide with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Vinsho Vinsho changed the title fix(postgres): read foreign keys from pg_catalog so read-only roles can navigate FKs fix(postgres): FK navigation for read-only roles and uuid keys Dec 1, 2026

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant