有表 customer_account
(postgres
) 是从 YII2 迁移过来的。
数据链接:
CREATE TABLE public.test_table (
id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('test_table_id_seq'::regclass),
data JSONB
);
在 go 项目中,我尝试从该表中获取值。
type TableGo struct {
Id int
Data string `gorm:"type:jsonb"`
}
table := TableGo{}
db.Where("id = ?", 75).Find(&table)
println(table.Data)
但是有(pq: relation "table_gos"does not exist)
我如何在没有 db.AutoMigrate(&TableGo{})
的情况下链接结构表?
最佳答案
我认为您的迁移脚本中的表名有误。因为它不在 GORM 约定中。如果你想使用那个名字,你可以在你的模型中使用下面的方法来自定义表名。
func (m *Model) TableName() string {
return "custom_table_name"
}
关于postgresql - 戈朗 : gorm use Find(&model) for non gorm migrate table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346924/