fix: add schema qualifier to cross-schema foreign key references (#94)#97
Merged
fix: add schema qualifier to cross-schema foreign key references (#94)#97
Conversation
When dumping a schema with foreign keys that reference tables in other schemas (e.g., tenant_schema.table → public.table), the REFERENCES clause was missing the schema qualifier. This caused: 1. Incorrect SQL output that depends on search_path 2. pgschema plan continuously generating plans to replace the constraint Root cause: generateConstraintSQL() ignored the targetSchema parameter and only used constraint.ReferencedTable without checking constraint.ReferencedSchema. Fix: Use ir.QualifyEntityNameWithQuotes() to add schema qualifier when the referenced table is in a different schema than the target schema. Enhanced testdata/dump/tenant/ test case to reproduce and prevent regression by adding a cross-schema FK from tenant schema to public.categories table. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where foreign key constraints referencing tables in different schemas were generated without schema qualifiers, causing incorrect SQL output and continuous plan generation. The fix ensures that cross-schema foreign key references include the proper schema qualifier (e.g., public.categories instead of just categories).
Key Changes:
- Modified
generateConstraintSQL()to use thetargetSchemaparameter and qualify referenced table names when they exist in different schemas - Added test case demonstrating cross-schema foreign key from tenant schema to public.categories table
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| testdata/dump/tenant/public.sql | Adds shared categories table in public schema for cross-schema FK testing |
| testdata/dump/tenant/pgschema.sql | Adds category_id column and cross-schema FK constraint to posts table |
| internal/diff/constraint.go | Implements schema qualification logic for foreign key references |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
- Added test case for cross-schema foreign key reference to verify schema qualifier is properly added when referencing tables in different schemas (e.g., tenant1.table -> public.table) - Fixed existing FK test by adding ReferencedSchema field to prevent empty schema qualifier (was generating ".users" instead of "users") - Updated test to use different target schemas to properly test both same-schema and cross-schema FK generation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #94
When dumping a schema with foreign keys that reference tables in other schemas (e.g., tenant_schema.table → public.table), the REFERENCES clause was missing the schema qualifier. This caused:
Root cause: generateConstraintSQL() ignored the targetSchema parameter and only used constraint.ReferencedTable without checking constraint.ReferencedSchema.
Fix: Use ir.QualifyEntityNameWithQuotes() to add schema qualifier when the referenced table is in a different schema than the target schema.
Enhanced testdata/dump/tenant/ test case to reproduce and prevent regression by adding a cross-schema FK from tenant schema to public.categories table.
🤖 Generated with Claude Code