Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
1e5051e1
Commit
1e5051e1
authored
Mar 05, 2017
by
astaxie
Committed by
GitHub
Mar 05, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2381 from chesedo/OrmStrongRelationships
Add strong relationship support to orm
parents
fa44ff54
82d2ace3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
0 deletions
+53
-0
db_utils.go
orm/db_utils.go
+2
-0
models_test.go
orm/models_test.go
+5
-0
orm.go
orm/orm.go
+2
-0
orm_test.go
orm/orm_test.go
+44
-0
No files found.
orm/db_utils.go
View file @
1e5051e1
...
...
@@ -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
!=
""
...
...
orm/models_test.go
View file @
1e5051e1
...
...
@@ -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
...
...
orm/orm.go
View file @
1e5051e1
...
...
@@ -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
()
}
...
...
orm/orm_test.go
View file @
1e5051e1
...
...
@@ -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"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment