mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 23:40:50 +00:00
Merge pull request #2381 from chesedo/OrmStrongRelationships
Add strong relationship support to orm
This commit is contained in:
commit
1e5051e112
@ -41,6 +41,8 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac
|
||||
vu := v.Int()
|
||||
exist = true
|
||||
value = vu
|
||||
} else if fi.fieldType&IsRelField > 0 {
|
||||
_, value, exist = getExistPk(fi.relModelInfo, reflect.Indirect(v))
|
||||
} else {
|
||||
vu := v.String()
|
||||
exist = vu != ""
|
||||
|
@ -406,6 +406,11 @@ type UintPk struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type PtrPk struct {
|
||||
ID *IntegerPk `orm:"pk;rel(one)"`
|
||||
Positive bool
|
||||
}
|
||||
|
||||
var DBARGS = struct {
|
||||
Driver string
|
||||
Source string
|
||||
|
@ -153,6 +153,8 @@ func (o *orm) ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, i
|
||||
id, vid := int64(0), ind.FieldByIndex(mi.fields.pk.fieldIndex)
|
||||
if mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
|
||||
id = int64(vid.Uint())
|
||||
} else if mi.fields.pk.rel {
|
||||
return o.ReadOrCreate(vid.Interface(), mi.fields.pk.relModelInfo.fields.pk.name)
|
||||
} else {
|
||||
id = vid.Int()
|
||||
}
|
||||
|
@ -193,6 +193,7 @@ func TestSyncDb(t *testing.T) {
|
||||
RegisterModel(new(InLineOneToOne))
|
||||
RegisterModel(new(IntegerPk))
|
||||
RegisterModel(new(UintPk))
|
||||
RegisterModel(new(PtrPk))
|
||||
|
||||
err := RunSyncdb("default", true, Debug)
|
||||
throwFail(t, err)
|
||||
@ -216,6 +217,7 @@ func TestRegisterModels(t *testing.T) {
|
||||
RegisterModel(new(InLineOneToOne))
|
||||
RegisterModel(new(IntegerPk))
|
||||
RegisterModel(new(UintPk))
|
||||
RegisterModel(new(PtrPk))
|
||||
|
||||
BootStrap()
|
||||
|
||||
@ -2144,6 +2146,48 @@ func TestUintPk(t *testing.T) {
|
||||
dORM.Delete(u)
|
||||
}
|
||||
|
||||
func TestPtrPk(t *testing.T) {
|
||||
parent := &IntegerPk{ID: 10, Value: "10"}
|
||||
|
||||
id, _ := dORM.Insert(parent)
|
||||
if !IsMysql {
|
||||
// MySql does not support last_insert_id in this case: see #2382
|
||||
throwFail(t, AssertIs(id, 10))
|
||||
}
|
||||
|
||||
ptr := PtrPk{ID: parent, Positive: true}
|
||||
num, err := dORM.InsertMulti(2, []PtrPk{ptr})
|
||||
throwFail(t, err)
|
||||
throwFail(t, AssertIs(num, 1))
|
||||
throwFail(t, AssertIs(ptr.ID, parent))
|
||||
|
||||
nptr := &PtrPk{ID: parent}
|
||||
created, pk, err := dORM.ReadOrCreate(nptr, "ID")
|
||||
throwFail(t, err)
|
||||
throwFail(t, AssertIs(created, false))
|
||||
throwFail(t, AssertIs(pk, 10))
|
||||
throwFail(t, AssertIs(nptr.ID, parent))
|
||||
throwFail(t, AssertIs(nptr.Positive, true))
|
||||
|
||||
nptr = &PtrPk{Positive: true}
|
||||
created, pk, err = dORM.ReadOrCreate(nptr, "Positive")
|
||||
throwFail(t, err)
|
||||
throwFail(t, AssertIs(created, false))
|
||||
throwFail(t, AssertIs(pk, 10))
|
||||
throwFail(t, AssertIs(nptr.ID, parent))
|
||||
|
||||
nptr.Positive = false
|
||||
num, err = dORM.Update(nptr)
|
||||
throwFail(t, err)
|
||||
throwFail(t, AssertIs(num, 1))
|
||||
throwFail(t, AssertIs(nptr.ID, parent))
|
||||
throwFail(t, AssertIs(nptr.Positive, false))
|
||||
|
||||
num, err = dORM.Delete(nptr)
|
||||
throwFail(t, err)
|
||||
throwFail(t, AssertIs(num, 1))
|
||||
}
|
||||
|
||||
func TestSnake(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"i": "i",
|
||||
|
Loading…
Reference in New Issue
Block a user