mirror of https://github.com/go-gorm/gorm.git
Merge pull request #666 from defend7/sqlite_autoincrement
Support the AUTOINCREMENT keyword on SQLite.
This commit is contained in:
commit
048963c568
|
@ -515,9 +515,18 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
||||||
func (scope *Scope) createTable() *Scope {
|
func (scope *Scope) createTable() *Scope {
|
||||||
var tags []string
|
var tags []string
|
||||||
var primaryKeys []string
|
var primaryKeys []string
|
||||||
|
var primaryKeyInColumnType bool = false
|
||||||
for _, field := range scope.GetStructFields() {
|
for _, field := range scope.GetStructFields() {
|
||||||
if field.IsNormal {
|
if field.IsNormal {
|
||||||
sqlTag := scope.generateSqlTag(field)
|
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)
|
tags = append(tags, scope.Quote(field.DBName)+" "+sqlTag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -528,7 +537,7 @@ func (scope *Scope) createTable() *Scope {
|
||||||
}
|
}
|
||||||
|
|
||||||
var primaryKeyStr string
|
var primaryKeyStr string
|
||||||
if len(primaryKeys) > 0 {
|
if len(primaryKeys) > 0 && !primaryKeyInColumnType {
|
||||||
primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ","))
|
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()
|
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:
|
case reflect.Bool:
|
||||||
return "bool"
|
return "bool"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
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"
|
return "integer"
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if autoIncrease {
|
if autoIncrease {
|
||||||
return "integer"
|
return "integer primary key autoincrement"
|
||||||
}
|
}
|
||||||
return "bigint"
|
return "bigint"
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
|
|
Loading…
Reference in New Issue