Skip to content
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

Add ability to set custom column types in the gorm tag, so things such as lengths can be added. #1176

Closed
wants to merge 0 commits into from

Conversation

RParkerHp
Copy link

This allows customisation in the same way as already implemented for data types via the WithDataTypeMap method.

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

What did this pull request do?

Adds the ability to customise the gorm tag's type field to add, for example, lengths.
This allows gorm:"type:char" to be set to gorm:"type:char(32)" (which isn't done by default for MS SQL Server, and possibly others).

User Case Description

An example of how this can be used is:

var columnMap = map[string]func(columnType gorm.ColumnType) (dataType string){
    "varchar":  getColumnTypeAndSize,
    "nvarchar": getColumnTypeAndSize,
    "nchar":    getColumnTypeAndSize,
    "char":     getColumnTypeAndSize,
}

cfg.WithColumnTypeMap(columnMap)

func getColumnTypeAndSize(columnType gorm.ColumnType) (dataType string) {
    typ, ok := columnType.ColumnType()
    if ok {
        len, ok := columnType.Length()
        if ok && len > 0 {
            return fmt.Sprintf("%v(%v)", typ, len)
        }
        return typ
    }
    return ""
}```

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.

1 participant