From d278ca49ef30f003c9624ae58d4d8726f728c1f7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 9 Apr 2021 11:43:24 +0800 Subject: [PATCH] sort GORM options before apply --- gorm.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gorm.go b/gorm.go index f1e3745f..0da218f6 100644 --- a/gorm.go +++ b/gorm.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "sort" "sync" "time" @@ -111,6 +112,12 @@ type Session struct { func Open(dialector Dialector, opts ...Option) (db *DB, err error) { config := &Config{} + sort.Slice(opts, func(i, j int) bool { + _, isConfig := opts[i].(*Config) + _, isConfig2 := opts[j].(*Config) + return isConfig && !isConfig2 + }) + for _, opt := range opts { if opt != nil { if err := opt.Apply(config); err != nil {