Skip to content

add tag function to (unique)index #7700

@holev45

Description

@holev45

Describe the feature

Currently, the only option for adding a functional index is to use an expression, which requires specifying the column name in the database. With dynamically generated names based on a custom naming strategy, this is very difficult. The mentioned tag could generate the appropriate expression during the parseFieldIndexes stage.

I create simple fork for my own:
gorm/schema/index.go

if settings["FUNCTION"] != "" && settings["EXPRESSION"] == "" {
f := strings.Split(settings["FUNCTION"], ":")
if len(f) == 1 {
settings["EXPRESSION"] = f[0] + "(" + field.DBName + ")"
} else {
settings["EXPRESSION"] = f[0] + "(" + field.DBName + ", " + f[1] + ")"
}
}

example:
function:upper
result:
upper("DB_COLUMN_NAME")

example:
function:coalesce:-1
result:
coalesce("DB_COLUMN_NAME", -1)

Motivation

Simplifying the creation of simple functional indexes with dynamically generated column names

Related Issues

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions