Skip to content

Commit

Permalink
Add mapping for remaining tags
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMacies committed Jul 12, 2024
1 parent 82bbce3 commit c5fa451
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (commonDialect) Quote(key string) string {
func (s *commonDialect) fieldCanAutoIncrement(field *StructField) bool {
if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
return strings.ToLower(value) != "false"
} else if value, ok := field.TagSettingsGet("AUTOINCREMENT"); ok {
return strings.ToLower(value) != "false"
}
return field.IsPrimaryKey
}
Expand Down
13 changes: 13 additions & 0 deletions model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
associationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
associationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("REFERENCES"); foreignKey != "" {
associationForeignKeys = strings.Split(foreignKey, ",")
}

for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
Expand All @@ -318,8 +320,11 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
{ // Foreign Keys for Source
joinTableDBNames := []string{}

// Compatibility with GORM v2
if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" {
joinTableDBNames = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("JOINFOREIGNKEY"); foreignKey != "" {
joinTableDBNames = strings.Split(foreignKey, ",")
}

// if no foreign keys defined with tag
Expand Down Expand Up @@ -351,6 +356,8 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *

if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_JOINTABLE_FOREIGNKEY"); foreignKey != "" {
associationJoinTableDBNames = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("JOINTABLEREFERENCES"); foreignKey != "" {
associationJoinTableDBNames = strings.Split(foreignKey, ",")
}

// if no association foreign keys defined with tag
Expand Down Expand Up @@ -395,8 +402,12 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName
// if Dog has multiple set of toys set name of the set (instead of default 'dogs')

// Compatibility with GORM v2
if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
relationship.PolymorphicValue = value
} else if value, ok := field.TagSettingsGet("POLYMORPHICVALUE"); ok {
relationship.PolymorphicValue = value
} else {
relationship.PolymorphicValue = scope.TableName()
}
Expand Down Expand Up @@ -489,6 +500,8 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
} else if foreignKey, _ := field.TagSettingsGet("REFERENCES"); foreignKey != "" {
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
}

if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
Expand Down

0 comments on commit c5fa451

Please sign in to comment.