Skip to content

Instantly share code, notes, and snippets.

@rtfb
Created July 9, 2014 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtfb/bc60c54f0c84792c5db4 to your computer and use it in GitHub Desktop.
Save rtfb/bc60c54f0c84792c5db4 to your computer and use it in GitHub Desktop.
Sample patch to add custom column names in gorm
diff --git a/main_test.go b/main_test.go
index 24e0118..1ab61c8 100644
--- a/main_test.go
+++ b/main_test.go
@@ -146,6 +146,12 @@ type Animal struct {
UpdatedAt time.Time
}
+type MappedFields struct {
+ Id int64 `name:"o_id"`
+ Name string `name:"o_name"`
+ Date time.Time `name:"o_time"`
+}
+
type Details struct {
Id int64
Bulk gorm.Hstore
diff --git a/scope.go b/scope.go
index dd8c3ac..6e93032 100644
--- a/scope.go
+++ b/scope.go
@@ -260,7 +260,13 @@ func (scope *Scope) Fields() []*Field {
var field Field
field.Name = fieldStruct.Name
- field.DBName = toSnake(fieldStruct.Name)
+ dbName := fieldStruct.Tag.Get("name")
+
+ if dbName != "" {
+ field.DBName = dbName
+ } else {
+ field.DBName = toSnake(fieldStruct.Name)
+ }
value := indirectValue.FieldByName(fieldStruct.Name)
field.Value = value.Interface()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment