fix insert single pk column table failed (#603)

This commit is contained in:
Lunny Xiao 2017-06-01 09:57:33 +08:00 committed by GitHub
parent 36a9cdaa23
commit 9c179e47a1
2 changed files with 30 additions and 8 deletions

View File

@ -343,15 +343,26 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
if len(exprColVals) > 0 {
colPlaces = colPlaces + strings.Join(exprColVals, ", ")
} else {
colPlaces = colPlaces[0 : len(colPlaces)-2]
if len(colPlaces) > 0 {
colPlaces = colPlaces[0 : len(colPlaces)-2]
}
}
sqlStr := fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)",
session.Engine.Quote(session.Statement.TableName()),
session.Engine.QuoteStr(),
strings.Join(colNames, session.Engine.Quote(", ")),
session.Engine.QuoteStr(),
colPlaces)
var sqlStr string
if len(colPlaces) > 0 {
sqlStr = fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)",
session.Engine.Quote(session.Statement.TableName()),
session.Engine.QuoteStr(),
strings.Join(colNames, session.Engine.Quote(", ")),
session.Engine.QuoteStr(),
colPlaces)
} else {
if session.Engine.dialect.DBType() == core.SQLITE || session.Engine.dialect.DBType() == core.POSTGRES {
sqlStr = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES", session.Engine.Quote(session.Statement.TableName()))
} else {
sqlStr = fmt.Sprintf("INSERT INTO %s VALUES ()", session.Engine.Quote(session.Statement.TableName()))
}
}
handleAfterInsertProcessorFunc := func(bean interface{}) {
if session.IsAutoCommit {
@ -384,7 +395,6 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
// for postgres, many of them didn't implement lastInsertId, so we should
// implemented it ourself.
if session.Engine.dialect.DBType() == core.ORACLE && len(table.AutoIncrement) > 0 {
//assert table.AutoIncrement != ""
res, err := session.query("select seq_atable.currval from dual", args...)
if err != nil {
return 0, err

View File

@ -1105,3 +1105,15 @@ func TestMyStringId(t *testing.T) {
panic(err)
}
}
func TestSingleAutoIncrColumn(t *testing.T) {
type Account struct {
Id int64 `xorm:"pk autoincr"`
}
assert.NoError(t, prepareEngine())
assertSync(t, new(Account))
_, err := testEngine.Insert(&Account{})
assert.NoError(t, err)
}