//我的模型是这样的
type paramExample3 struct {
V身份证号 string `gorm:"column:person_num;primaryKey"`
V学校编号 string `gorm:"column:school_num;uniqueIndex:udx_student_unique"`
V班级编号 string `gorm:"column:class_num;uniqueIndex:udx_student_unique"`
V班内排名 string `gorm:"column:student_num;uniqueIndex:udx_student_unique"`
V姓名 string `gorm:"column:name;index"` //普通索引,默认名称不正确(现在的默认名称带中文)
V年龄 int `gorm:"column:age;unique"` //唯一约束,而非唯一索引,默认名称正确,使用的还是 uni_param_example3_age 这个约束名
V性别 bool `gorm:"column:sex;uniqueIndex"` //唯一索引,带名称,默认名称不正确(现在的默认名称带中文)
}
//我的逻辑是这样的
package gormmom
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/yyle88/done"
"github.com/yyle88/runpath"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func TestDryRunMigrate(t *testing.T) {
db := done.VCE(gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})).Nice()
defer func() {
done.Done(done.VCE(db.DB()).Nice().Close())
}()
require.NoError(t, db.Session(&gorm.Session{
DryRun: true,
}).AutoMigrate(¶mExample3{}))
}
//运行结果是这样的:
=== RUN TestDryRunMigrate
2024/08/12 15:01:14 /Users/admin/go-projects/open/gormmom/idxname_test.go:43
[0.024ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="param_example3"
CREATE TABLE `param_example3` (`person_num` text,`school_num` text,`class_num` text,`student_num` text,`name` text,`age` integer,`sex` numeric,PRIMARY KEY (`person_num`),CONSTRAINT `uni_param_example3_age` UNIQUE (`age`));
2024/08/12 15:01:14 /Users/admin/go-projects/open/gormmom/idxname_test.go:43
[0.004ms] [rows:0] CREATE TABLE `param_example3` (`person_num` text,`school_num` text,`class_num` text,`student_num` text,`name` text,`age` integer,`sex` numeric,PRIMARY KEY (`person_num`),CONSTRAINT `uni_param_example3_age` UNIQUE (`age`))
CREATE UNIQUE INDEX `idx_param_example3_v性���` ON `param_example3`(`sex`);
2024/08/12 15:01:14 /Users/admin/go-projects/open/gormmom/idxname_test.go:43
[0.001ms] [rows:0] CREATE UNIQUE INDEX `idx_param_example3_v性���` ON `param_example3`(`sex`)
CREATE INDEX `idx_param_example3_v姓���` ON `param_example3`(`name`);
2024/08/12 15:01:14 /Users/admin/go-projects/open/gormmom/idxname_test.go:43
[0.001ms] [rows:0] CREATE INDEX `idx_param_example3_v姓���` ON `param_example3`(`name`)
CREATE UNIQUE INDEX `udx_student_unique` ON `param_example3`(`school_num`,`class_num`,`student_num`);
2024/08/12 15:01:14 /Users/admin/go-projects/open/gormmom/idxname_test.go:43
[0.001ms] [rows:0] CREATE UNIQUE INDEX `udx_student_unique` ON `param_example3`(`school_num`,`class_num`,`student_num`)
--- PASS: TestDryRunMigrate (0.00s)
PASS
进程 已完成,退出代码为 0
因此问题就在这里,既然已经指定了列名,那为啥在单列索引的时候,用的还是字段名呢?如代码所示,这就很尴尬,在我以为我可以天才般的以中文写代码大大降低理解难度(以及规避我刚过六级的贫穷英语水平,和解决起名真难的困境)的时候,遭遇到这个问题。
当然我也可以理解您的代码逻辑:
func (ns NamingStrategy) toDBName(name string) string {
if name == "" {
return ""
}
if ns.NameReplacer != nil {
tmpName := ns.NameReplacer.Replace(name)
if tmpName == "" {
return name
}
name = tmpName
}
但是为啥设计时,这块的调用参数,传的是个模型中的字段名,而不是 column name 呢,总感觉这样怪怪的。
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: yangyile1990
我的意思是,我的索引是建立在db里的,假如我有一个字段叫A,但我给他的 column name 是 "b" 这时候,按照现有逻辑,建立的索引是 idx_tablename_a 而不是 idx_tablename_b,这就在db侧看到的,很不直观,而在 gorm 运行时看到建立索引名 idx_tablename_a 其实也不符合用户的第一预期。
Comment From: yangyile1990
this is the code in this file: gorm/schema/index.go
if name == "" {
subName := field.Name
const key = "COMPOSITE"
if composite, found := settings[key]; found {
if len(composite) == 0 || composite == key {
err = fmt.Errorf(
"The composite tag of %s.%s cannot be empty",
field.Schema.Name,
field.Name)
return
}
subName = composite
}
name = field.Schema.namer.IndexName(
field.Schema.Table, subName)
}
so why the subName := field.Name as default value rather than use the subName := field.DBName?
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨