Skip to content

Conversation

@tianzhou
Copy link
Contributor

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:

  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

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>
Copilot AI review requested due to automatic review settings October 19, 2025 07:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 the targetSchema parameter 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>
@tianzhou tianzhou merged commit cb798c0 into main Oct 19, 2025
2 checks passed
@tianzhou tianzhou deleted the cross_schema_fk_qualifier branch October 23, 2025 06:34
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.

foreign key constraints which reference a table in another schema are not fully qualified in dump

1 participant