我是 golang 的新手。我正在尝试使用 GORM 和数据库/sql 包写入特定的数据库模式。 这是我的结构
type Person struct {
gorm.Model
Name string
Age int
}
我在数据库中编写的函数是:
func writedb(){
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+" password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
db, err := gorm.Open("postgres", psqlInfo)
if err != nil {
panic(err)
fmt.Println("Não conectou-se ao BANCO DE DADOS")
}
defer db.Close()
db.AutoMigrate(&Person{})
//t := time.Now()
//ts := t.Format("2006-01-02 15:04:05")
db.Create(&Person{Name : "alex", Age: 20})
}
我的数据库结构如下 数据库名称 --schemaPeople --schemaVehicle --schemaPublic
当我编译时,插入的数据转到公共(public)模式中的一个新表,我想在人员模式中插入一行。我究竟做错了什么?我是在声明结构错误吗?如何设置特定架构??
最佳答案
在 gorm
中,您可以在结构的 TableName()
方法中表示模式,例如:
type Person struct {
gorm.Model
Name string
Age int
}
func (Person) TableName() string {
return "people.persons"
}
关于database - 如何使用 gorm 写入特定的数据库模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54214697/