Skip to content

fix(cli): db pull missing relation name if one model has both to-one and to-many relation for another model#2761

Open
jiashengguo wants to merge 2 commits into
devfrom
jiasheng-fix
Open

fix(cli): db pull missing relation name if one model has both to-one and to-many relation for another model#2761
jiashengguo wants to merge 2 commits into
devfrom
jiasheng-fix

Conversation

@jiashengguo

@jiashengguo jiashengguo commented Jul 19, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Fixed database pulls to preserve opposite-direction relations between the same two tables.
    • Improved relation matching by refining how referenced columns and schemas are compared (including MySQL-style null/empty schema equivalence).
  • Tests

    • Expanded and adjusted db pull test coverage across PostgreSQL, MySQL, and SQLite.
    • Updated generated-column assertions to match current output formatting.

@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

The relation synchronization predicate now normalizes schema comparisons and no longer requires the first referenced column to match. db pull tests cover opposite-direction relations and updated formatter output across PostgreSQL, MySQL, and SQLite.

Database pull behavior

Layer / File(s) Summary
Preserve opposite-direction relations
packages/cli/src/actions/db.ts, packages/cli/test/db/pull.test.ts
Relation similarity uses normalized schema/table references without comparing the first referenced column, and a regression test covers opposite-direction relations.
Refresh pull test expectations
packages/cli/test/db/pull.test.ts
Provider-specific generated-column assertions now match current formatDocument(...) output, with related setup and call-formatting updates.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: ymc9

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main db pull fix for missing relation names in opposite-direction relations between the same models.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jiasheng-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jiashengguo

Copy link
Copy Markdown
Member Author

@copilot The CI failed because of the new added test case 'should preserve opposite-direction relations between the same two tables' failed for mysql. Please help fix it

Copilot AI commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

@copilot The CI failed because of the new added test case 'should preserve opposite-direction relations between the same two tables' failed for mysql. Please help fix it

Fixed in commit fix(cli): normalize schema comparisons to handle MySQL null vs empty string.

The root cause: MySQL's introspection returns null for references.schema (foreign key schema) but '' (empty string) for table.schema. The reverse-direction relation matching check rr.schema === relation.references.schema evaluated as '' === nullfalse, so the two opposite relations between the same tables were never recognized as similar.

The fix adds a schemasMatch helper that normalizes null, undefined, and '' as equivalent when comparing schema values, applied consistently to all schema comparisons in the relation matching logic.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/cli/src/actions/db.ts`:
- Around line 178-190: Update the similarRelations filter in the
resolvedRelations counting logic to compare relation-column identity in addition
to schema and table matches. Preserve the existing direction-aware branches,
requiring each branch to match the corresponding local and referenced column
arrays while keeping reversed relations mapped to the opposite arrays.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4d6eba26-592e-4356-bbd8-56856371d207

📥 Commits

Reviewing files that changed from the base of the PR and between b6e08f1 and 8e5664d.

📒 Files selected for processing (1)
  • packages/cli/src/actions/db.ts

Comment on lines 178 to 190
const similarRelations = resolvedRelations.filter((rr) => {
return (
rr !== relation &&
((rr.schema === relation.schema &&
((schemasMatch(rr.schema, relation.schema) &&
rr.table === relation.table &&
rr.references.schema === relation.references.schema &&
schemasMatch(rr.references.schema, relation.references.schema) &&
rr.references.table === relation.references.table) ||
(rr.schema === relation.references.schema &&
rr.columns[0] === relation.references.columns[0] &&
rr.references.schema === relation.schema &&
(schemasMatch(rr.schema, relation.references.schema) &&
rr.table === relation.references.table &&
schemasMatch(rr.references.schema, relation.schema) &&
rr.references.table === relation.table))
);
}).length;

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.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Preserve relation-column identity when counting similar relations.

This now treats every relation between the same table pair as similar, even when they use different foreign-key columns (for example, createdById versus updatedById). That can make unrelated relations share the same similarity count and cause incorrect relation-name synchronization. Keep a direction-aware comparison of the local and referenced column arrays in addition to the schema/table checks.

Suggested direction-aware check
+const columnsMatch = (a: string[], b: string[]) =>
+    a.length === b.length && a.every((column, index) => column === b[index]);
+
 const similarRelations = resolvedRelations.filter((rr) => {
     return (
         rr !== relation &&
         ((schemasMatch(rr.schema, relation.schema) &&
             rr.table === relation.table &&
+            columnsMatch(rr.columns, relation.columns) &&
             schemasMatch(rr.references.schema, relation.references.schema) &&
-            rr.references.table === relation.references.table) ||
+            rr.references.table === relation.references.table &&
+            columnsMatch(rr.references.columns, relation.references.columns)) ||
             (schemasMatch(rr.schema, relation.references.schema) &&
                 rr.table === relation.references.table &&
+                columnsMatch(rr.columns, relation.references.columns) &&
                 schemasMatch(rr.references.schema, relation.schema) &&
-                rr.references.table === relation.table))
+                rr.references.table === relation.table &&
+                columnsMatch(rr.references.columns, relation.columns)))
     );
});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const similarRelations = resolvedRelations.filter((rr) => {
return (
rr !== relation &&
((rr.schema === relation.schema &&
((schemasMatch(rr.schema, relation.schema) &&
rr.table === relation.table &&
rr.references.schema === relation.references.schema &&
schemasMatch(rr.references.schema, relation.references.schema) &&
rr.references.table === relation.references.table) ||
(rr.schema === relation.references.schema &&
rr.columns[0] === relation.references.columns[0] &&
rr.references.schema === relation.schema &&
(schemasMatch(rr.schema, relation.references.schema) &&
rr.table === relation.references.table &&
schemasMatch(rr.references.schema, relation.schema) &&
rr.references.table === relation.table))
);
}).length;
const columnsMatch = (a: string[], b: string[]) =>
a.length === b.length && a.every((column, index) => column === b[index]);
const similarRelations = resolvedRelations.filter((rr) => {
return (
rr !== relation &&
((schemasMatch(rr.schema, relation.schema) &&
rr.table === relation.table &&
columnsMatch(rr.columns, relation.columns) &&
schemasMatch(rr.references.schema, relation.references.schema) &&
rr.references.table === relation.references.table &&
columnsMatch(rr.references.columns, relation.references.columns)) ||
(schemasMatch(rr.schema, relation.references.schema) &&
rr.table === relation.references.table &&
columnsMatch(rr.columns, relation.references.columns) &&
schemasMatch(rr.references.schema, relation.schema) &&
rr.references.table === relation.table &&
columnsMatch(rr.references.columns, relation.columns)))
);
}).length;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/cli/src/actions/db.ts` around lines 178 - 190, Update the
similarRelations filter in the resolvedRelations counting logic to compare
relation-column identity in addition to schema and table matches. Preserve the
existing direction-aware branches, requiring each branch to match the
corresponding local and referenced column arrays while keeping reversed
relations mapped to the opposite arrays.

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.

2 participants