Skip to content

dolthub/dolt#9496 - Fix DECIMAL foreign key constraint validation to match MySQL behavior #3094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

elianddb
Copy link
Contributor

Fixes dolthub/dolt#9496
Allow DECIMAL foreign key creation with different precision/scale but enforce strict constraint validation
MySQL allows DECIMAL foreign keys with different precision/scale but rejects constraint violations based on exact scale matching

elianddb and others added 11 commits July 11, 2025 22:08
Allow foreign key references between DECIMAL columns with different precision/scale to match MySQL behavior.

- Modified foreignKeyComparableTypes() to allow DECIMAL types with different precision/scale
- Updated test expectations to reflect correct behavior with current decimal implementation
- All foreign key tests pass

Fixes dolthub/dolt#9496

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Added DECIMAL foreign key compatibility test suite to foreign_key_queries.go
- Tests foreign key creation between DECIMAL columns with different precision/scale
- Tests constraint validation with both valid and invalid inserts
- Validates that the fix works for various DECIMAL type combinations

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Added logic to detect DECIMAL precision/scale mismatches
- DECIMAL types are not ExtendedType so type conversion approach doesn't work
- Current implementation partially working but needs refinement
- MySQL correctly rejects 78.9 (4,1) -> 78.90 (4,2) FK references
- Need to find alternative approach for DECIMAL type comparison

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Allow DECIMAL foreign key creation with different precision/scale
- Add strict constraint validation that rejects values with different scales
- Use existing sql.DecimalType interface and Index.ColumnExpressionTypes() methods
- Ensure 78.9 DECIMAL(4,1) \!= 78.90 DECIMAL(4,2) like MySQL
- Clean implementation using existing constructors and functions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove verbose comments and dead code
- Reduce nesting in shouldRejectDecimalMatch function
- Use existing patterns from codebase
- Remove unnecessary vitess import
- Simplify type checking logic

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Extract foreignKeyHasDifferentDecimalScales as centralized helper
- Can be reused by other parts of FK validation logic
- Follows existing patterns for FK column iteration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Rename foreignKeyHasDifferentDecimalScales to validateForeignKeyDecimalScales
- Follows existing naming pattern in codebase

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove unnecessary function separation since it's only used in one place
- Keep logic contained within shouldRejectDecimalMatch method
- Simplify code structure

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Rename shouldRejectDecimalMatch to validateDecimalMatch
- Consistent with existing codebase validate naming scheme
- Update function call to use new name

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
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.

DECIMALs with foreign key behavior differs from MySQL
1 participant