-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat: Add optional case-insensitive matching for schema field lookup #7457
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
base: master
Are you sure you want to change the base?
Conversation
Hi @joboon How about setting up a different NamingStrategy for each database? |
Hi, @jinzhu! Thanks for taking a look and reaching out. Admittedly, I'm not super familiar with all the customizability within gorm. However, from what I can tell, the NamingStrategy controls the Go -> SQL conversions. I'm less worried about that, since the database (Oracle, for example) will automatically adjust the casing for me when interpreting the SQL. However, in the SQL -> Go conversion in gorm, Oracle will return results with uppercased table/column names and they won't be mapped to the schema fields based on a case-agnostic struct tag. This PR is to add optional case insensitivity when attempting to match those names with the names defined by the structs (without having to tie us to a specific DB's casing implementation for all queries). Am I missing something in your recommendation? Have I misunderstood how NamingStrategy objects work? Thanks again for your time! |
Hi, @jinzhu. Any more ideas for me here? |
I also need this feature. We have many fields with irregular naming conventions, and we hope that when defining the structure, we only need to define lowercase field names to match the database fields. |
There may be tables with the same name and structure in PG and MySQL databases, but the field names may be all uppercase in PG and all lowercase in MySQL. We hope to use the same structure to mapping the fields |
@jinzhu, any more thoughts on this one? Thanks! |
aa0e8e7
to
c446e3f
Compare
Add Opt-In Case-Insensitive Schema Field Matching This pull request introduces support for optional case-insensitive matching of schema fields (table and column names) within GORM. A new configuration option, Key Changes• Added Affected Areas• schema/schema.go This summary was automatically generated by @propel-code-bot |
if schema.FieldsCaseInsensitive { | ||
for key, field := range schema.FieldsByDBName { | ||
if strings.EqualFold(key, name) { | ||
return field | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[PerformanceOptimization]
The case insensitive field lookup implementation may cause performance degradation when iterating over potentially large field maps. Consider implementing a more efficient solution using lowercase keys in maps for case insensitive comparisons.
if schema.FieldsCaseInsensitive { | |
for key, field := range schema.FieldsByDBName { | |
if strings.EqualFold(key, name) { | |
return field | |
} | |
} | |
} | |
// Consider adding a separate map for case insensitive lookups during schema initialization | |
// For example: fieldsByLowerDBName map[string]*Field | |
// This would provide O(1) lookups instead of O(n) iteration |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Bumping this PR. Thanks. |
What did this pull request do?
Adds a new configuration option to ignore case sensitivity when associating table and column names with the target schema. Requires opting in. The default behavior is unchanged.
User Case Description
As a developer supporting an application which supports multiple databases, all with different default casing and case sensitivity implementations, I want to use structs with consistent naming from a Go perspective while keeping table and column names consistent with the casing for the target database. This avoids the need to quote all future interactions with the database to match a specific casing. In order to preserve the current behavior, matching schema fields without case sensitivity should be optional.