From dd92f8bdc0ba926a538dce7a84fd3b630d45c168 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 23 Oct 2020 11:01:45 +0800 Subject: [PATCH] Allow create table for other database/schema #3640 --- migrator/migrator.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/migrator/migrator.go b/migrator/migrator.go index c455a294..9493a00c 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -32,6 +32,7 @@ func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error stmt := &gorm.Statement{DB: m.DB} if m.DB.Statement != nil { stmt.Table = m.DB.Statement.Table + stmt.TableExpr = m.DB.Statement.TableExpr } if table, ok := value.(string); ok { @@ -161,6 +162,10 @@ func (m Migrator) CreateTable(values ...interface{}) error { hasPrimaryKeyInDataType bool ) + if stmt.TableExpr != nil { + values[0] = *stmt.TableExpr + } + for _, dbName := range stmt.Schema.DBNames { field := stmt.Schema.FieldsByDBName[dbName] createTableSQL += "? ?"