Skip to content

fix(postgres): preserve composite foreign key column order - #1002

Merged
huydo862003 merged 1 commit into
holistics:masterfrom
sean0407:codex/ordered-composite-fk-columns
Oct 14, 2026
Merged

huydo862003 merged 1 commit into
holistics:masterfrom
sean0407:codex/ordered-composite-fk-columns

Conversation

@sean0407

Copy link
Copy Markdown
Contributor

Summary

PostgreSQL foreign-key metadata now keeps referencing and referenced columns paired in the constraint's declared order.

pg_constraint.conkey + confkey
  -> unnest with ordinality
  -> ordered column aggregation
  -> aligned Ref endpoints

Issue

Closes #991

Lasting Changes (Technical)

  • Build PostgreSQL foreign-key endpoints from paired constraint column numbers.
  • Add a regression test that creates a composite foreign key whose source and target names sort in different orders.

Checklist

  • Documentation (not needed)
  • Tests (integration test/unit test)
  • Integration Tests Passed
  • Code Review

Evidence

  • Before: The regression test failed against upstream master: the referenced columns came back as ["alpha", "zeta"] instead of the declared ["zeta", "alpha"].
  • After: The same test passed against an isolated PostgreSQL 16 instance. The connector package build passed. ESLint reported 0 errors; its only warning is in the unchanged Oracle connector file.

Merge Danger

Door: Two-way.

Blast Radius: PostgreSQL connector.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T08:46:10.851700Z bc3969c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bc3969cd04

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
END AS on_update
FROM pg_constraint AS constraint_row

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Filter catalog constraints to tables visible to the role

When the connection role can access only part of a selected schema, pg_constraint and pg_attribute are still readable for the other tables, while generateTablesAndFields gets only privilege-visible rows from information_schema.columns. This therefore appends refs whose endpoint tables were not exported; importer.generateDbml rejects such an endpoint as a missing table, so db2dbml can stop producing output for least-privilege connections. Preserve the former information-schema privilege predicate or filter refs to exported tables.

Useful? React with 👍 / 👎.

ON target_column.attrelid = target_table.oid
AND target_column.attnum = key_column.target_attnum
WHERE constraint_row.contype = 'f'
AND source_ns.nspname NOT IN ('pg_catalog', 'information_schema')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exclude foreign keys from other sessions' temp schemas

When a fetch is run without schemas=... while another session has temporary tables with a foreign key, those constraints are present in pg_constraint under pg_temp_*, and this namespace check does not remove them. The information-schema views still used for tables and fields omit other sessions' temp schemas, so the returned ref points at tables absent from the result and downstream DBML generation fails or depends on unrelated session state. Add NOT pg_is_other_temp_schema(source_ns.oid) to retain the prior behavior.

Useful? React with 👍 / 👎.

@huydo862003
huydo862003 merged commit 9ffe17a into holistics:master Oct 14, 2026
2 of 4 checks passed
@huydo862003

Copy link
Copy Markdown
Contributor

Thank you for your contribution!

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.

db2dbml - Postgres connector pairs composite foreign key columns incorrectly

2 participants