Skip to content

fix(postgres): support cross-schema foreign keys in related records and navigation (#1473) - #1478

Closed
markankamaransheogarat-sys wants to merge 1 commit into
TabularisDB:mainfrom
markankamaransheogarat-sys:fix/postgres-cross-schema-foreign-keys-1473
Closed

markankamaransheogarat-sys wants to merge 1 commit into
TabularisDB:mainfrom
markankamaransheogarat-sys:fix/postgres-cross-schema-foreign-keys-1473

Conversation

@markankamaransheogarat-sys

@markankamaransheogarat-sys markankamaransheogarat-sys commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1473

Problem

When a table in one PostgreSQL schema (e.g. billing.invoices) has a foreign key referencing a table in another schema (e.g. core.organizations), the Related Records panel and Open referenced table in new tab look for billing.organizations instead of core.organizations, failing with:
ERROR: relation "billing.organizations" does not exist.

Root Cause

  1. models::ForeignKey in src-tauri lacked a ref_schema field, dropping ref_nsp.nspname::text AS foreign_schema_name which was already returned by the PostgreSQL driver query.
  2. The UI (useReferencedRecord, Editor.tsx, NewRowModal, DataGrid, sqlGenerator) relied solely on the current tab's active schema when resolving referenced tables instead of checking if fk.ref_schema was provided.

Solution

  • Rust Backend:
    • Added pub ref_schema: Option<String> to ForeignKey in models.rs (with #[serde(skip_serializing_if = "Option::is_none")]).
    • Mapped foreign_schema_name to ref_schema in get_foreign_keys and get_all_foreign_keys_batch in the PostgreSQL driver.
    • Enabled cross-schema foreign key integration test assertions in foreign_keys.rs and parity_foreign_keys.rs.
  • TypeScript / Web UI:
    • Added ref_schema?: string | null to ForeignKey type.
    • In useReferencedRecord.ts, resolved targetSchema = fk.ref_schema ?? schema when quoting the table and in the execute_query RPC payload.
    • In Editor.tsx (handleForeignKeyNavigate), set targetSchema = activeCapabilities?.schemas ? (fk.ref_schema ?? currentTab.schema) : undefined.
    • Updated related record panels, modals, context menu, and SQL generator to respect ref_schema.
    • Added unit test coverage for useReferencedRecord and sqlGenerator.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Comment thread packages/web-ui/src/pages/Editor.tsx Outdated
title: currentTab.database
? `${fk.ref_table} (${currentTab.database}${targetSchema ? `.${targetSchema}` : ''})`
: fk.ref_table,
: (targetSchema ? `${fk.ref_table} (${targetSchema})` : fk.ref_table),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Tab title changes for all schema-aware drivers, not just cross-schema FKs

When currentTab.database is unset, targetSchema is now always truthy for Postgres (it falls back to currentTab.schema), so every FK navigation produces a title like organizations (public) instead of organizations. This is a UI behaviour change beyond the cross-schema fix, and may break title-based tab dedup/lookup or existing tests. Consider adding the schema suffix only when fk.ref_schema differs from currentTab.schema. Also the added parentheses around the ternary are redundant.

});
const columns = renderIdentifiers(ordered.map((c) => c.name));
const refColumns = renderIdentifiers(ordered.map((c) => c.ref_column));
const refTableRef = group.ref_schema

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Generated DDL now schema-qualifies same-schema references

The Postgres driver always returns ref_schema (even for same-schema FKs), so generateCreateTableSQL will now emit REFERENCES "public"."tbl" for every FK while the created table itself (CREATE TABLE "tbl") stays unqualified. This is valid SQL but changes existing generated output and is inconsistent. Consider qualifying only when ref_schema differs from the table's own schema, or document the intended behaviour.

@kilo-code-bot

kilo-code-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Incremental review of 3ed8d05..b25cbf6. Both previously reported findings are resolved:

  • Editor.tsx: tab title no longer gets a schema suffix for same-schema FK navigation.
  • sqlGenerator.ts: generated CREATE TABLE DDL no longer schema-qualifies FK references (and the corresponding test was removed).

The get_foreign_keys_orders.json golden now includes ref_schema, consistent with the Postgres driver always populating ref_schema from foreign_schema_name.

Files Reviewed (4 files)
  • packages/web-ui/src/pages/Editor.tsx
  • packages/web-ui/src/utils/sqlGenerator.ts
  • packages/web-ui/tests/utils/sqlGenerator.test.ts
  • src-tauri/tests/postgres_integration/golden/get_foreign_keys_orders.json
Previous Review Summary (commit 3ed8d05)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 3ed8d05)

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 1
Issue Details (click to expand)

WARNING

File Line Issue
packages/web-ui/src/pages/Editor.tsx 3274 Tab title now always gets a (schema) suffix for schema-aware drivers (Postgres always supplies ref_schema/currentTab.schema), not only for cross-schema FKs; redundant parentheses too.

SUGGESTION

File Line Issue
packages/web-ui/src/utils/sqlGenerator.ts 289 Generated CREATE TABLE DDL now schema-qualifies same-schema FK references while the table itself is unqualified; changes existing output and is inconsistent.
Files Reviewed (20 files)
  • packages/web-ui/src/components/layout/sidebar/SidebarTableItem.tsx
  • packages/web-ui/src/components/modals/NewRowModal.tsx
  • packages/web-ui/src/components/modals/SchemaObjectDetailsModal.tsx
  • packages/web-ui/src/components/ui/DataGrid.tsx
  • packages/web-ui/src/components/ui/DataGridCell.tsx
  • packages/web-ui/src/components/ui/RelatedRecordsPanel.tsx
  • packages/web-ui/src/hooks/useReferencedRecord.ts
  • packages/web-ui/src/pages/Editor.tsx - 1 issue
  • packages/web-ui/src/types/schema.ts
  • packages/web-ui/src/utils/sqlGenerator.ts - 1 issue
  • packages/web-ui/tests/hooks/useReferencedRecord.test.ts
  • packages/web-ui/tests/utils/sqlGenerator.test.ts
  • src-tauri/src/ai_schema_context_tests.rs
  • src-tauri/src/drivers/mysql/mod.rs
  • src-tauri/src/drivers/postgres/mod.rs
  • src-tauri/src/drivers/sqlite/mod.rs
  • src-tauri/src/models.rs
  • src-tauri/tests/postgres_integration/foreign_keys.rs
  • src-tauri/tests/postgres_integration/golden/get_foreign_keys_cross_schema.json
  • src-tauri/tests/postgres_integration/parity_foreign_keys.rs

Fix these issues in Kilo Cloud


Reviewed by claude-sonnet-5.5 · Input: 252 · Output: 1.6K · Cached: 210.6K

Review guidance: REVIEW.md from base branch main

…nd navigation (TabularisDB#1473)

Signed-off-by: markankamaransheogarat-sys <279850453+markankamaransheogarat-sys@users.noreply.github.com>
@markankamaransheogarat-sys
markankamaransheogarat-sys force-pushed the fix/postgres-cross-schema-foreign-keys-1473 branch from 3ed8d05 to b25cbf6 Compare November 12, 2026 08:40

@1-DARK 1-DARK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, @markankamaransheogarat-sys ! The core change looks good: ref_schema is mapped from the existing foreign_schema_name column, and the UI falls back cleanly with fk.ref_schema ?? schema.

I checked locally that your new useReferencedRecord test fails on main (it queries "billing"."organizations") and passes on this branch. Typecheck, lint, cargo check --no-default-features, and the full Vitest suite also pass.

One change needed before merge:

In packages/web-ui/src/pages/Editor.tsx (L3272–3274), the tab title for foreign-key navigation dropped the schema:

${fk.ref_table} (${db}.${schema}) became ${fk.ref_table} (${db}).

With cross-schema foreign keys, core.organizations and billing.organizations now open as two tabs that are both titled organizations (mydb).

Could you restore the previous expression? It already uses your new targetSchema:

? `${fk.ref_table} (${currentTab.database}${targetSchema ? `.${targetSchema}` : ''})`

Optional / nice to have:

  • The description mentions sqlGenerator changes and tests, but they aren't in the diff. Could you update the description or add the change? generateForeignKeyConstraints still emits an unqualified REFERENCES. That's existing behaviour, so a follow-up is fine.
  • A small test for the schema and title produced by handleForeignKeyNavigate would help prevent this regression.
  • FYI: the reporter uses the external PostgreSQL plugin, which will also need to send ref_schema.

debba
debba previously requested changes Nov 12, 2026

@debba debba left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded: closing this as a duplicate of #1474, see the comment below.

@debba
debba dismissed their stale review November 12, 2026 22:02

Closed as a duplicate of #1474.

@debba

debba commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Thanks a lot @markankamaransheogarat-sys for the time you put into this. #1474 fixes the same issue with the same backend change and was opened a few hours earlier, so I've merged that one and I'm closing this as a duplicate. Your catch on the New Row modal FK dropdown was a good one, and it's being picked up as a follow-up to #1474. Thanks again, and see you on the next one!

@debba debba closed this Nov 12, 2026
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.

[Bug]: Related Records fails for foreign keys that reference another PostgreSQL schema

3 participants