Added separate function to generate primary keys
This commit is contained in:
parent
e84e58d3c0
commit
133176c3fb
@ -20,6 +20,8 @@ type Dialect interface {
|
|||||||
BindVar(i int) string
|
BindVar(i int) string
|
||||||
// Quote quotes field name to avoid SQL parsing exceptions by using a reserved word as a field name
|
// Quote quotes field name to avoid SQL parsing exceptions by using a reserved word as a field name
|
||||||
Quote(key string) string
|
Quote(key string) string
|
||||||
|
// PrimaryKeys used to define the primary keys of a table
|
||||||
|
PrimaryKeys(keys []string) string
|
||||||
// DataTypeOf return data's sql type
|
// DataTypeOf return data's sql type
|
||||||
DataTypeOf(field *StructField) string
|
DataTypeOf(field *StructField) string
|
||||||
|
|
||||||
|
@ -32,6 +32,10 @@ func (commonDialect) Quote(key string) string {
|
|||||||
return fmt.Sprintf(`"%s"`, key)
|
return fmt.Sprintf(`"%s"`, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (commonDialect) PrimaryKeys(keys []string) string {
|
||||||
|
return fmt.Sprintf("PRIMARY KEY (%v)", strings.Join(keys, ","))
|
||||||
|
}
|
||||||
|
|
||||||
func (commonDialect) DataTypeOf(field *StructField) string {
|
func (commonDialect) DataTypeOf(field *StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
|
||||||
|
|
||||||
|
@ -42,6 +42,10 @@ func (mssql) Quote(key string) string {
|
|||||||
return fmt.Sprintf(`"%s"`, key)
|
return fmt.Sprintf(`"%s"`, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mssql) PrimaryKeys(keys []string) string {
|
||||||
|
return fmt.Sprintf("PRIMARY KEY (%v)", strings.Join(keys, ","))
|
||||||
|
}
|
||||||
|
|
||||||
func (mssql) DataTypeOf(field *gorm.StructField) string {
|
func (mssql) DataTypeOf(field *gorm.StructField) string {
|
||||||
var dataValue, sqlType, size, additionalType = gorm.ParseFieldStructForDialect(field)
|
var dataValue, sqlType, size, additionalType = gorm.ParseFieldStructForDialect(field)
|
||||||
|
|
||||||
|
15
scope.go
15
scope.go
@ -80,6 +80,17 @@ func (scope *Scope) Quote(str string) string {
|
|||||||
return scope.Dialect().Quote(str)
|
return scope.Dialect().Quote(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrimaryKeys used to define the primary keys of a table
|
||||||
|
func (scope *Scope) PrimaryKeys(keys []string) string {
|
||||||
|
res := scope.Dialect().PrimaryKeys(keys)
|
||||||
|
|
||||||
|
if res != "" {
|
||||||
|
return fmt.Sprintf(", %v", res)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// Err add error to Scope
|
// Err add error to Scope
|
||||||
func (scope *Scope) Err(err error) error {
|
func (scope *Scope) Err(err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1027,7 +1038,7 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v, PRIMARY KEY (%v)) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), strings.Join(primaryKeys, ","), scope.getTableOptions())).Error)
|
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.PrimaryKeys(primaryKeys), scope.getTableOptions())).Error)
|
||||||
}
|
}
|
||||||
scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
|
scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
|
||||||
}
|
}
|
||||||
@ -1059,7 +1070,7 @@ func (scope *Scope) createTable() *Scope {
|
|||||||
|
|
||||||
var primaryKeyStr string
|
var primaryKeyStr string
|
||||||
if len(primaryKeys) > 0 && !primaryKeyInColumnType {
|
if len(primaryKeys) > 0 && !primaryKeyInColumnType {
|
||||||
primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ","))
|
primaryKeyStr = scope.PrimaryKeys(primaryKeys)
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec()
|
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user