Merge pull request #666 from defend7/sqlite_autoincrement

Support the AUTOINCREMENT keyword on SQLite.
This commit is contained in:
Jinzhu 2015-09-28 10:20:17 +08:00
commit 048963c568
2 changed files with 14 additions and 2 deletions

View File

@ -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()

View File

@ -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: