mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 23:00:55 +00:00
Merge pull request #1810 from miraclesu/fix/orm_miss_pk
orm: fix miss pk when pk is negative
This commit is contained in:
commit
ba7a809de8
@ -181,7 +181,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
switch {
|
switch {
|
||||||
case fi.fieldType&IsPostiveIntegerField > 0:
|
case fi.fieldType&IsPositiveIntegerField > 0:
|
||||||
if field.Kind() == reflect.Ptr {
|
if field.Kind() == reflect.Ptr {
|
||||||
if field.IsNil() {
|
if field.IsNil() {
|
||||||
value = nil
|
value = nil
|
||||||
@ -516,7 +516,7 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
|
|||||||
}
|
}
|
||||||
if num > 0 {
|
if num > 0 {
|
||||||
if mi.fields.pk.auto {
|
if mi.fields.pk.auto {
|
||||||
if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
|
if mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
|
||||||
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetUint(0)
|
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetUint(0)
|
||||||
} else {
|
} else {
|
||||||
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetInt(0)
|
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetInt(0)
|
||||||
@ -1140,7 +1140,7 @@ setValue:
|
|||||||
tErr = err
|
tErr = err
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
if fieldType&IsPostiveIntegerField > 0 {
|
if fieldType&IsPositiveIntegerField > 0 {
|
||||||
v, _ := str.Uint64()
|
v, _ := str.Uint64()
|
||||||
value = v
|
value = v
|
||||||
} else {
|
} else {
|
||||||
@ -1292,7 +1292,7 @@ setValue:
|
|||||||
field.Set(reflect.ValueOf(&v))
|
field.Set(reflect.ValueOf(&v))
|
||||||
}
|
}
|
||||||
case fieldType&IsIntegerField > 0:
|
case fieldType&IsIntegerField > 0:
|
||||||
if fieldType&IsPostiveIntegerField > 0 {
|
if fieldType&IsPositiveIntegerField > 0 {
|
||||||
if isNative {
|
if isNative {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
value = uint64(0)
|
value = uint64(0)
|
||||||
|
@ -33,13 +33,13 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac
|
|||||||
fi := mi.fields.pk
|
fi := mi.fields.pk
|
||||||
|
|
||||||
v := ind.FieldByIndex(fi.fieldIndex)
|
v := ind.FieldByIndex(fi.fieldIndex)
|
||||||
if fi.fieldType&IsPostiveIntegerField > 0 {
|
if fi.fieldType&IsPositiveIntegerField > 0 {
|
||||||
vu := v.Uint()
|
vu := v.Uint()
|
||||||
exist = vu > 0
|
exist = vu > 0
|
||||||
value = vu
|
value = vu
|
||||||
} else if fi.fieldType&IsIntegerField > 0 {
|
} else if fi.fieldType&IsIntegerField > 0 {
|
||||||
vu := v.Int()
|
vu := v.Int()
|
||||||
exist = vu > 0
|
exist = true
|
||||||
value = vu
|
value = vu
|
||||||
} else {
|
} else {
|
||||||
vu := v.String()
|
vu := v.String()
|
||||||
|
@ -47,7 +47,7 @@ const (
|
|||||||
// Define some logic enum
|
// Define some logic enum
|
||||||
const (
|
const (
|
||||||
IsIntegerField = ^-TypePositiveBigIntegerField >> 4 << 5
|
IsIntegerField = ^-TypePositiveBigIntegerField >> 4 << 5
|
||||||
IsPostiveIntegerField = ^-TypePositiveBigIntegerField >> 8 << 9
|
IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 8 << 9
|
||||||
IsRelField = ^-RelReverseMany >> 14 << 15
|
IsRelField = ^-RelReverseMany >> 14 << 15
|
||||||
IsFieldType = ^-RelReverseMany<<1 + 1
|
IsFieldType = ^-RelReverseMany<<1 + 1
|
||||||
)
|
)
|
||||||
|
@ -387,6 +387,11 @@ func NewInLineOneToOne() *InLineOneToOne {
|
|||||||
return new(InLineOneToOne)
|
return new(InLineOneToOne)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IntegerPk struct {
|
||||||
|
Id int64 `orm:"pk"`
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
var DBARGS = struct {
|
var DBARGS = struct {
|
||||||
Driver string
|
Driver string
|
||||||
Source string
|
Source string
|
||||||
|
@ -159,7 +159,7 @@ func (o *orm) Insert(md interface{}) (int64, error) {
|
|||||||
// set auto pk field
|
// set auto pk field
|
||||||
func (o *orm) setPk(mi *modelInfo, ind reflect.Value, id int64) {
|
func (o *orm) setPk(mi *modelInfo, ind reflect.Value, id int64) {
|
||||||
if mi.fields.pk.auto {
|
if mi.fields.pk.auto {
|
||||||
if mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
|
if mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
|
||||||
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetUint(uint64(id))
|
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetUint(uint64(id))
|
||||||
} else {
|
} else {
|
||||||
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetInt(id)
|
ind.FieldByIndex(mi.fields.pk.fieldIndex).SetInt(id)
|
||||||
|
@ -50,7 +50,7 @@ func (o *insertSet) Insert(md interface{}) (int64, error) {
|
|||||||
}
|
}
|
||||||
if id > 0 {
|
if id > 0 {
|
||||||
if o.mi.fields.pk.auto {
|
if o.mi.fields.pk.auto {
|
||||||
if o.mi.fields.pk.fieldType&IsPostiveIntegerField > 0 {
|
if o.mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
|
||||||
ind.FieldByIndex(o.mi.fields.pk.fieldIndex).SetUint(uint64(id))
|
ind.FieldByIndex(o.mi.fields.pk.fieldIndex).SetUint(uint64(id))
|
||||||
} else {
|
} else {
|
||||||
ind.FieldByIndex(o.mi.fields.pk.fieldIndex).SetInt(id)
|
ind.FieldByIndex(o.mi.fields.pk.fieldIndex).SetInt(id)
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -189,6 +190,7 @@ func TestSyncDb(t *testing.T) {
|
|||||||
RegisterModel(new(GroupPermissions))
|
RegisterModel(new(GroupPermissions))
|
||||||
RegisterModel(new(InLine))
|
RegisterModel(new(InLine))
|
||||||
RegisterModel(new(InLineOneToOne))
|
RegisterModel(new(InLineOneToOne))
|
||||||
|
RegisterModel(new(IntegerPk))
|
||||||
|
|
||||||
err := RunSyncdb("default", true, Debug)
|
err := RunSyncdb("default", true, Debug)
|
||||||
throwFail(t, err)
|
throwFail(t, err)
|
||||||
@ -210,6 +212,7 @@ func TestRegisterModels(t *testing.T) {
|
|||||||
RegisterModel(new(GroupPermissions))
|
RegisterModel(new(GroupPermissions))
|
||||||
RegisterModel(new(InLine))
|
RegisterModel(new(InLine))
|
||||||
RegisterModel(new(InLineOneToOne))
|
RegisterModel(new(InLineOneToOne))
|
||||||
|
RegisterModel(new(IntegerPk))
|
||||||
|
|
||||||
BootStrap()
|
BootStrap()
|
||||||
|
|
||||||
@ -1991,3 +1994,22 @@ func TestInLineOneToOne(t *testing.T) {
|
|||||||
throwFail(t, AssertIs(rinline.Name, name))
|
throwFail(t, AssertIs(rinline.Name, name))
|
||||||
throwFail(t, AssertIs(rinline.Email, email))
|
throwFail(t, AssertIs(rinline.Email, email))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIntegerPk(t *testing.T) {
|
||||||
|
its := []IntegerPk{
|
||||||
|
{Id: math.MinInt64, Value: "-"},
|
||||||
|
{Id: 0, Value: "0"},
|
||||||
|
{Id: math.MaxInt64, Value: "+"},
|
||||||
|
}
|
||||||
|
|
||||||
|
num, err := dORM.InsertMulti(len(its), its)
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(num, len(its)))
|
||||||
|
|
||||||
|
for _, intPk := range its {
|
||||||
|
out := IntegerPk{Id: intPk.Id}
|
||||||
|
err = dORM.Read(&out)
|
||||||
|
throwFail(t, err)
|
||||||
|
throwFail(t, AssertIs(out.Value, intPk.Value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user