fix: Fix PostgreSQL numeric type mapping in goctl model generation #4992
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 PostgreSQL numeric type mapping in goctl model generation
Environment
Description
This PR fixes an issue where PostgreSQL
numericanddecimaltypes are incorrectly mapped todoublein the PostgreSQL to MySQL type mapping, preventing users from properly customizing type mappings for high-precision numeric fields.Problem
When using
goctl modelcommand to generate ORM code,numerictype fields are mapped tofloat64. While this works in most cases, it causes issues when users try to customize type mappings according to the [official documentation](https://go-zero.dev/en/docs/tutorials/cli/model#type-mapping-customization).Reproduction Steps
numerictype:goctl modelcommand with the custom configuration:goctl model pg datasource -url "postgres://user:password@localhost/db" -table users -cdecimal.Decimaltypefloat64type, custom mapping is ignoredRoot Cause
The current mapping in
postgresqlmodel.goincorrectly maps high-precision types:This hardcoded mapping converts PostgreSQL
numericto MySQLdoubletype, which then gets mapped to Go'sfloat64. This prevents the custom type mapping from taking effect, as the hardcoded mapping takes precedence.PostgreSQL
numericanddecimaltypes are high-precision types (similar to MySQL'sdecimal), and should not be automatically converted todoubleas this can lead to precision loss and user confusion.Solution
numericanddecimaltypes from thep2mmapdefault.yamlto provide clear guidance for usersChanges Made
Updated
postgresqlmodel.go:"numeric": "double"mapping"decimal": "double"mappingUpdated
default.yaml:numericanddecimaltypesTesting
numericfields are no longer automatically mapped todoubleImpact
Before
After
Breaking Changes
This change is non-breaking as it only removes incorrect automatic mappings and allows proper customization. Existing code that relies on the default
float64mapping can continue to work by explicitly defining the mapping in their configuration.Documentation Impact
This fix aligns the behavior with the official documentation and makes the type mapping customization feature work as intended.
Checklist
Related Issues
Fixes the issue where PostgreSQL
numerictype mapping customization was not working as documented.Type of Change: Bug Fix
Priority: Medium
Reviewers: @go-zero/maintainers