如何使用 gorm 写入特定的数据库模式?

How to write in a specif db schema with gorm?

我是 golang 的新手。我正在尝试使用 GORM 和 database/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 模式中的新 table,我想在人员模式中插入一行。我究竟做错了什么?我是在声明结构错误吗?如何设置特定架构??

gorm 中,您可以在结构的 TableName() 方法中表示模式,例如:

type Person struct {
    gorm.Model
    Name string
    Age  int
}

func (Person) TableName() string {
    return "people.persons"
}