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

新增自定义创建索引功能 #2

Open
steden opened this issue Jan 17, 2024 · 0 comments
Open

新增自定义创建索引功能 #2

steden opened this issue Jan 17, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@steden
Copy link
Member

steden commented Jan 17, 2024

在gorm组件中,支持对PO层的tag进行index定义,以此实现索引的自动维护。
但当组合索引比较多时,此种方式不易阅读和后期的维护。
现增加一种新的方式:
在PO层,定义CreateIndex(),以此实现接口:

type IMigratorIndex interface {
	// CreateIndex 创建索引
	CreateIndex() map[string][]string
}

示例:

type AccountPO struct {
	Id               int                  `gorm:"primaryKey;autoIncrement;comment:账户ID"`
	AccountName      string               `gorm:"size:32;not null;comment:账号名称"`
}

// 创建索引
func (*AccountPO) CreateIndex() map[string][]string {
	return map[string][]string{
		"idx_name":  {"account_name", "id"}
	}
}

将在框架启动时,创建索引名称为:idx_name,字段为account_name、id 的组合索引。

@steden steden added the documentation Improvements or additions to documentation label Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant