mirror of https://github.com/go-gorm/gorm.git
Support the AUTOINCREMENT keyword on SQLite.
Omit the "PRIMARY KEY" clause at the end of the create statement if any column's SQL tags already contains the PRIMARY KEY constraint.
This commit is contained in:
parent
19aacb8fbb
commit
1d8292c5ab
|
@ -512,9 +512,18 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||
func (scope *Scope) createTable() *Scope {
|
||||
var tags []string
|
||||
var primaryKeys []string
|
||||
var primaryKeyInColumnType bool = false
|
||||
for _, field := range scope.GetStructFields() {
|
||||
if field.IsNormal {
|
||||
sqlTag := scope.generateSqlTag(field)
|
||||
|
||||
// Check if the primary key constraint was specified as
|
||||
// part of the column type. If so, we can only support
|
||||
// one column as the primary key.
|
||||
if strings.Contains(strings.ToLower(sqlTag), "primary key") {
|
||||
primaryKeyInColumnType = true
|
||||
}
|
||||
|
||||
tags = append(tags, scope.Quote(field.DBName)+" "+sqlTag)
|
||||
}
|
||||
|
||||
|
@ -525,7 +534,7 @@ func (scope *Scope) createTable() *Scope {
|
|||
}
|
||||
|
||||
var primaryKeyStr string
|
||||
if len(primaryKeys) > 0 {
|
||||
if len(primaryKeys) > 0 && !primaryKeyInColumnType {
|
||||
primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ","))
|
||||
}
|
||||
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec()
|
||||
|
|
|
@ -15,10 +15,13 @@ func (sqlite3) SqlTag(value reflect.Value, size int, autoIncrease bool) string {
|
|||
case reflect.Bool:
|
||||
return "bool"
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||
if autoIncrease {
|
||||
return "integer primary key autoincrement"
|
||||
}
|
||||
return "integer"
|
||||
case reflect.Int64, reflect.Uint64:
|
||||
if autoIncrease {
|
||||
return "integer"
|
||||
return "integer primary key autoincrement"
|
||||
}
|
||||
return "bigint"
|
||||
case reflect.Float32, reflect.Float64:
|
||||
|
|
Loading…
Reference in New Issue