1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 15:01:31 +00:00

Merge pull request #1828 from miraclesu/fix/orm_read_or_create

orm: fix painc when pk is uint on ReadOrCreate
This commit is contained in:
astaxie 2016-03-25 14:44:41 +08:00
commit 4dbbae61e0
4 changed files with 47 additions and 8 deletions

View File

@ -123,14 +123,16 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) {
} }
// make returning sql support for postgresql. // make returning sql support for postgresql.
func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) { func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) bool {
if mi.fields.pk.auto { fi := mi.fields.pk
if fi.fieldType&IsPositiveIntegerField == 0 && fi.fieldType&IsIntegerField == 0 {
return false
}
if query != nil { if query != nil {
*query = fmt.Sprintf(`%s RETURNING "%s"`, *query, mi.fields.pk.column) *query = fmt.Sprintf(`%s RETURNING "%s"`, *query, fi.column)
} }
has = true return true
}
return
} }
// show table sql for postgresql. // show table sql for postgresql.

View File

@ -392,6 +392,11 @@ type IntegerPk struct {
Value string Value string
} }
type UintPk struct {
Id uint32 `orm:"pk"`
Name string
}
var DBARGS = struct { var DBARGS = struct {
Driver string Driver string
Source string Source string

View File

@ -140,7 +140,14 @@ func (o *orm) ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, i
return (err == nil), id, err return (err == nil), id, err
} }
return false, ind.FieldByIndex(mi.fields.pk.fieldIndex).Int(), err id, vid := int64(0), ind.FieldByIndex(mi.fields.pk.fieldIndex)
if mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
id = int64(vid.Uint())
} else {
id = vid.Int()
}
return false, id, err
} }
// insert model data to database // insert model data to database

View File

@ -191,6 +191,7 @@ func TestSyncDb(t *testing.T) {
RegisterModel(new(InLine)) RegisterModel(new(InLine))
RegisterModel(new(InLineOneToOne)) RegisterModel(new(InLineOneToOne))
RegisterModel(new(IntegerPk)) RegisterModel(new(IntegerPk))
RegisterModel(new(UintPk))
err := RunSyncdb("default", true, Debug) err := RunSyncdb("default", true, Debug)
throwFail(t, err) throwFail(t, err)
@ -213,6 +214,7 @@ func TestRegisterModels(t *testing.T) {
RegisterModel(new(InLine)) RegisterModel(new(InLine))
RegisterModel(new(InLineOneToOne)) RegisterModel(new(InLineOneToOne))
RegisterModel(new(IntegerPk)) RegisterModel(new(IntegerPk))
RegisterModel(new(UintPk))
BootStrap() BootStrap()
@ -2013,3 +2015,26 @@ func TestIntegerPk(t *testing.T) {
throwFail(t, AssertIs(out.Value, intPk.Value)) throwFail(t, AssertIs(out.Value, intPk.Value))
} }
} }
func TestUintPk(t *testing.T) {
name := "go"
u := &UintPk{
Id: 8,
Name: name,
}
created, pk, err := dORM.ReadOrCreate(u, "Id")
throwFail(t, err)
throwFail(t, AssertIs(created, true))
throwFail(t, AssertIs(u.Name, name))
nu := &UintPk{Id: 8}
created, pk, err = dORM.ReadOrCreate(nu, "Id")
throwFail(t, err)
throwFail(t, AssertIs(created, false))
throwFail(t, AssertIs(nu.Id, u.Id))
throwFail(t, AssertIs(pk, u.Id))
throwFail(t, AssertIs(nu.Name, name))
dORM.Delete(u)
}