Your Question

When I use the same model to create tables with different table names like in the example below

var tableTplName = ""

type Person struct {
    Id         uint64    `gorm:"column:id;primary_key;AUTO_INCREMENT" json:"id"`
}

func (p *Person) TableName() string {
    return tableTplName
}

func main() {
    initMySql()

    tableTplName = "test_4"
    if err := MySql.Migrator().CreateTable(&Person{}); err != nil {
        fmt.Println(err)
    }

    tableTplName = "test_5"
    if err := MySql.Migrator().CreateTable(&Person{}); err != nil {
        fmt.Println(err)
    }
}

The table name test_5 can not be create, the following results will be displayed

Error 1050: Table 'test_4' already exists

The document you expected this should be explained

Expected answer

I know that the TableName() method will be cached, but I don't know of other ways to create different tables using the same model, currently I can only do it with native SQL

Comment From: a631807682

DB.Table("user_1").Migrator().CreateTable(&User{})