From 680a51761b529b57f662bb01454c4502582309fa Mon Sep 17 00:00:00 2001 From: a631807682 <631807682@qq.com> Date: Wed, 6 Mar 2024 13:54:32 +0800 Subject: [PATCH] fix: namer identifier max length --- postgres.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/postgres.go b/postgres.go index 50bb307..e865b0f 100644 --- a/postgres.go +++ b/postgres.go @@ -48,19 +48,25 @@ func (dialector Dialector) Name() string { } func (dialector Dialector) Apply(config *gorm.Config) error { - var namingStartegy *schema.NamingStrategy + if config.NamingStrategy == nil { + config.NamingStrategy = schema.NamingStrategy{ + IdentifierMaxLength: defaultIdentifierLength, + } + return nil + } + switch v := config.NamingStrategy.(type) { case *schema.NamingStrategy: - namingStartegy = v + if v.IdentifierMaxLength <= 0 { + v.IdentifierMaxLength = defaultIdentifierLength + } case schema.NamingStrategy: - namingStartegy = &v - case nil: - namingStartegy = &schema.NamingStrategy{} - } - if namingStartegy.IdentifierMaxLength <= 0 { - namingStartegy.IdentifierMaxLength = defaultIdentifierLength + if v.IdentifierMaxLength <= 0 { + v.IdentifierMaxLength = defaultIdentifierLength + config.NamingStrategy = v + } } - config.NamingStrategy = namingStartegy + return nil }