Skip to content

Conversation

joboon
Copy link

@joboon joboon commented May 15, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

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.

@joboon joboon marked this pull request as ready for review May 15, 2025 17:08
@jinzhu
Copy link
Member

jinzhu commented May 23, 2025

Hi @joboon

How about setting up a different NamingStrategy for each database?

@joboon
Copy link
Author

joboon commented May 23, 2025

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!

@joboon
Copy link
Author

joboon commented Jun 3, 2025

Hi, @jinzhu. Any more ideas for me here?

@KeenBash
Copy link

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.

@KeenBash
Copy link

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

@joboon
Copy link
Author

joboon commented Jul 7, 2025

@jinzhu, any more thoughts on this one? Thanks!

@joboon joboon force-pushed the CaseInsensitiveSchemaFields branch from aa0e8e7 to c446e3f Compare September 2, 2025 20:54
@propel-code-bot propel-code-bot bot changed the title feat: support case insensitive schema fields feat: Add optional case-insensitive matching for schema field lookup Sep 2, 2025
Copy link

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, CaseInsensitiveSchemaFields, is made available at both the global Config and per-Session levels. When enabled, field lookups in schema operations will ignore name casing discrepancies, addressing issues encountered when working with databases that differ in their treatment of identifier casing (e.g., Oracle vs. PostgreSQL). Default behavior remains case-sensitive for backward compatibility.

Key Changes

• Added CaseInsensitiveSchemaFields boolean to Config and Session for opt-in case-insensitivity.
• Updated schema methods (LookUpField, LookUpFieldByBindName, schema parsing) to optionally use case-insensitive matching when configured.
• Adjusted statement parsing, schema relationship resolution, field parsing, scanning, and query-building to respect CaseInsensitiveSchemaFields.
• Introduced new or refactored schema parse methods to propagate the case-sensitivity flag during schema/model parsing.
• Added and expanded tests to verify both case-sensitive and case-insensitive lookup behaviors.
• Updated module dependencies (added testify and related test dependencies).

Affected Areas

• schema/schema.go
• schema/field.go
• schema/relationship.go
• schema/schema_test.go
• gorm.go
• statement.go
• scan.go
finisher_api.go

This summary was automatically generated by @propel-code-bot

Comment on lines +83 to +89
if schema.FieldsCaseInsensitive {
for key, field := range schema.FieldsByDBName {
if strings.EqualFold(key, name) {
return field
}
}
}

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.

Suggested change
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.

@joboon
Copy link
Author

joboon commented Sep 2, 2025

Bumping this PR. Thanks.

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.

3 participants