Your Question
GORM (or perhaps the postgres driver) doesn't seem to support clauses on AutoMigrate. In my case, I implemented my own clause to add a partition statement after table creation. (ie. CREATE TABLE .... PARTITION BY RANGE (date);). To rule out that my implementation is incorrect, I attempted to use hints that is referenced in the docs.
DB, err := gorm.Open(postgres.Open(connStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
log.Fatal(err)
}
type Log struct {
Date time.Time
Content string
}
err = DB.Clauses(hints.CommentAfter("CREATE", "hello!")).AutoMigrate(&Log{})
// Expected: CREATE TABLE "logs" ("date" timestamptz,"content" text) /*hello!*/
or
// Expected: CREATE /*hello!*/ TABLE "logs" ("date" timestamptz,"content" text)
// Actual: CREATE TABLE "logs" ("date" timestamptz,"content" text)
Granted the raw SQL that is spit out is from the logger.
gorm.io/driver/postgres v1.2.3
gorm.io/gorm v1.22.4
gorm.io/hints v1.1.0
The document you expected this should be explained
https://gorm.io/docs/sql_builder.html#Advanced
Expected answer
Are clauses not supported for auto migration? If not, what would be the suggested way to insert a clause during table creation using the Migrator interface?
My end goal is really something like
CREATE TABLE "logs" ("date" timestamptz,"content" text) PARTITION BY RANGE (date);
Perhaps there is another way to do this? Trying to avoid raw sql
Comment From: jinzhu
Use "gorm:table_options" refer https://gorm.io/docs/migration.html
Comment From: Nashluffy
Thanks @jinzhu! - here's what worked
if err = DB.Set("gorm:table_options", "PARTITION BY RANGE(date)").AutoMigrate(&Log{}); err != nil {
log.Fatal(err)
}