fix: model unique keys generated differently in each re-generation (#1771)

master
Kevin Wan 3 years ago committed by GitHub
parent 0cc9d4ff8d
commit 415c4c91fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
package gen package gen
import ( import (
"sort"
"strings" "strings"
"github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/core/collection"
@ -19,6 +20,10 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
keySet.AddStr(key.DataKeyExpression) keySet.AddStr(key.DataKeyExpression)
keyVariableSet.AddStr(key.KeyLeft) keyVariableSet.AddStr(key.KeyLeft)
} }
keys := keySet.KeysStr()
sort.Strings(keys)
keyVars := keyVariableSet.KeysStr()
sort.Strings(keyVars)
camel := table.Name.ToCamel() camel := table.Name.ToCamel()
text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete) text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete)
@ -34,9 +39,9 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
"containsIndexCache": table.ContainsUniqueCacheKey, "containsIndexCache": table.ContainsUniqueCacheKey,
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()), "lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
"dataType": table.PrimaryKey.DataType, "dataType": table.PrimaryKey.DataType,
"keys": strings.Join(keySet.KeysStr(), "\n"), "keys": strings.Join(keys, "\n"),
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), "keyValues": strings.Join(keyVars, ", "),
"postgreSql": postgreSql, "postgreSql": postgreSql,
"data": table, "data": table,
}) })

@ -82,7 +82,8 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
} }
if withCache { if withCache {
text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod) text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile,
template.FindOneByFieldExtraMethod)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -2,6 +2,7 @@ package gen
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/core/collection"
@ -20,6 +21,10 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
keySet.AddStr(key.DataKeyExpression) keySet.AddStr(key.DataKeyExpression)
keyVariableSet.AddStr(key.KeyLeft) keyVariableSet.AddStr(key.KeyLeft)
} }
keys := keySet.KeysStr()
sort.Strings(keys)
keyVars := keyVariableSet.KeysStr()
sort.Strings(keyVars)
expressions := make([]string, 0) expressions := make([]string, 0)
expressionValues := make([]string, 0) expressionValues := make([]string, 0)
@ -59,8 +64,8 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
"lowerStartCamelObject": stringx.From(camel).Untitle(), "lowerStartCamelObject": stringx.From(camel).Untitle(),
"expression": strings.Join(expressions, ", "), "expression": strings.Join(expressions, ", "),
"expressionValues": strings.Join(expressionValues, ", "), "expressionValues": strings.Join(expressionValues, ", "),
"keys": strings.Join(keySet.KeysStr(), "\n"), "keys": strings.Join(keys, "\n"),
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), "keyValues": strings.Join(keyVars, ", "),
"data": table, "data": table,
}) })
if err != nil { if err != nil {

@ -1,6 +1,7 @@
package gen package gen
import ( import (
"sort"
"strings" "strings"
"github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/core/collection"
@ -33,6 +34,10 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
keySet.AddStr(key.DataKeyExpression) keySet.AddStr(key.DataKeyExpression)
keyVariableSet.AddStr(key.KeyLeft) keyVariableSet.AddStr(key.KeyLeft)
} }
keys := keySet.KeysStr()
sort.Strings(keys)
keyVars := keyVariableSet.KeysStr()
sort.Strings(keyVars)
if postgreSql { if postgreSql {
expressionValues = append([]string{"data." + table.PrimaryKey.Name.ToCamel()}, expressionValues...) expressionValues = append([]string{"data." + table.PrimaryKey.Name.ToCamel()}, expressionValues...)
@ -50,8 +55,8 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
Execute(map[string]interface{}{ Execute(map[string]interface{}{
"withCache": withCache, "withCache": withCache,
"upperStartCamelObject": camelTableName, "upperStartCamelObject": camelTableName,
"keys": strings.Join(keySet.KeysStr(), "\n"), "keys": strings.Join(keys, "\n"),
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), "keyValues": strings.Join(keyVars, ", "),
"primaryCacheKey": table.PrimaryCacheKey.DataKeyExpression, "primaryCacheKey": table.PrimaryCacheKey.DataKeyExpression,
"primaryKeyVariable": table.PrimaryCacheKey.KeyLeft, "primaryKeyVariable": table.PrimaryCacheKey.KeyLeft,
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(), "lowerStartCamelObject": stringx.From(camelTableName).Untitle(),

Loading…
Cancel
Save