fix(cli): db pull missing relation name if one model has both to-one and to-many relation for another model#2761
fix(cli): db pull missing relation name if one model has both to-one and to-many relation for another model#2761jiashengguo wants to merge 2 commits into
Conversation
…and to-many relation for another model
📝 WalkthroughWalkthroughChangesThe relation synchronization predicate now normalizes schema comparisons and no longer requires the first referenced column to match. Database pull behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
@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 The root cause: MySQL's introspection returns The fix adds a |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
packages/cli/src/actions/db.ts
| 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; |
There was a problem hiding this comment.
🎯 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.
| 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.
Summary by CodeRabbit
Bug Fixes
Tests
db pulltest coverage across PostgreSQL, MySQL, and SQLite.