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
e5096be3
Commit
e5096be3
authored
Dec 17, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1490 from pjoe/orm_pk_rel_many
Fix joins for reverse(many) with custom pk
parents
9b87f528
906637ae
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
1 deletion
+89
-1
db_tables.go
orm/db_tables.go
+1
-1
models_test.go
orm/models_test.go
+18
-0
orm_test.go
orm/orm_test.go
+70
-0
No files found.
orm/db_tables.go
View file @
e5096be3
...
...
@@ -186,7 +186,7 @@ func (t *dbTables) getJoinSQL() (join string) {
table
=
jt
.
mi
.
table
switch
{
case
jt
.
fi
.
fieldType
==
RelManyToMany
||
jt
.
fi
.
reverse
&&
jt
.
fi
.
reverseFieldInfo
.
fieldType
==
RelManyToMany
:
case
jt
.
fi
.
fieldType
==
RelManyToMany
||
jt
.
fi
.
fieldType
==
RelReverseMany
||
jt
.
fi
.
reverse
&&
jt
.
fi
.
reverseFieldInfo
.
fieldType
==
RelManyToMany
:
c1
=
jt
.
fi
.
mi
.
fields
.
pk
.
column
for
_
,
ffi
:=
range
jt
.
mi
.
fields
.
fieldsRel
{
if
jt
.
fi
.
mi
==
ffi
.
relModelInfo
{
...
...
orm/models_test.go
View file @
e5096be3
...
...
@@ -332,6 +332,24 @@ func NewComment() *Comment {
return
obj
}
type
Group
struct
{
GID
string
`orm:"pk;column(gid);size(32);unique"`
Name
string
Permissions
[]
*
Permission
`orm:"reverse(many)" json:"-"`
}
type
Permission
struct
{
ID
int
`orm:"column(id)"`
Name
string
Groups
[]
*
Group
`orm:"rel(m2m);rel_through(github.com/astaxie/beego/orm.GroupPermissions)"`
}
type
GroupPermissions
struct
{
ID
int
`orm:"column(id)"`
Group
*
Group
`orm:"rel(fk)"`
Permission
*
Permission
`orm:"rel(fk)"`
}
var
DBARGS
=
struct
{
Driver
string
Source
string
...
...
orm/orm_test.go
View file @
e5096be3
...
...
@@ -35,6 +35,19 @@ var (
testDateTime
=
formatDateTime
+
" -0700"
)
type
argAny
[]
interface
{}
// get interface by index from interface slice
func
(
a
argAny
)
Get
(
i
int
,
args
...
interface
{})
(
r
interface
{})
{
if
i
>=
0
&&
i
<
len
(
a
)
{
r
=
a
[
i
]
}
if
len
(
args
)
>
0
{
r
=
args
[
0
]
}
return
}
func
ValuesCompare
(
is
bool
,
a
interface
{},
args
...
interface
{})
(
ok
bool
,
err
error
)
{
if
len
(
args
)
==
0
{
return
false
,
fmt
.
Errorf
(
"miss args"
)
...
...
@@ -171,6 +184,9 @@ func TestSyncDb(t *testing.T) {
RegisterModel
(
new
(
Comment
))
RegisterModel
(
new
(
UserBig
))
RegisterModel
(
new
(
PostTags
))
RegisterModel
(
new
(
Group
))
RegisterModel
(
new
(
Permission
))
RegisterModel
(
new
(
GroupPermissions
))
err
:=
RunSyncdb
(
"default"
,
true
,
false
)
throwFail
(
t
,
err
)
...
...
@@ -187,6 +203,9 @@ func TestRegisterModels(t *testing.T) {
RegisterModel
(
new
(
Comment
))
RegisterModel
(
new
(
UserBig
))
RegisterModel
(
new
(
PostTags
))
RegisterModel
(
new
(
Group
))
RegisterModel
(
new
(
Permission
))
RegisterModel
(
new
(
GroupPermissions
))
BootStrap
()
...
...
@@ -635,6 +654,45 @@ The program—and web server—godoc processes Go source files to extract docume
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
>
0
,
true
))
}
permissions
:=
[]
*
Permission
{
{
Name
:
"writePosts"
},
{
Name
:
"readComments"
},
{
Name
:
"readPosts"
},
}
groups
:=
[]
*
Group
{
{
GID
:
"g1"
,
Name
:
"admins"
,
Permissions
:
[]
*
Permission
{
permissions
[
0
],
permissions
[
1
],
permissions
[
2
]},
},
{
GID
:
"g2"
,
Name
:
"users"
,
Permissions
:
[]
*
Permission
{
permissions
[
1
],
permissions
[
2
]},
},
}
for
_
,
permission
:=
range
permissions
{
id
,
err
:=
dORM
.
Insert
(
permission
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
>
0
,
true
))
}
for
_
,
group
:=
range
groups
{
id
,
err
:=
dORM
.
Insert
(
group
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
>
0
,
true
))
num
:=
len
(
group
.
Permissions
)
if
num
>
0
{
nums
,
err
:=
dORM
.
QueryM2M
(
group
,
"permissions"
)
.
Add
(
group
.
Permissions
)
throwFailNow
(
t
,
err
)
throwFailNow
(
t
,
AssertIs
(
nums
,
num
))
}
}
}
func
TestCustomField
(
t
*
testing
.
T
)
{
...
...
@@ -1398,6 +1456,18 @@ func TestQueryRelate(t *testing.T) {
// throwFailNow(t, AssertIs(num, 2))
}
func
TestPkManyRelated
(
t
*
testing
.
T
)
{
permission
:=
&
Permission
{
Name
:
"readPosts"
}
err
:=
dORM
.
Read
(
permission
,
"Name"
)
throwFailNow
(
t
,
err
)
var
groups
[]
*
Group
qs
:=
dORM
.
QueryTable
(
"Group"
)
num
,
err
:=
qs
.
Filter
(
"Permissions__Permission"
,
permission
.
ID
)
.
All
(
&
groups
)
throwFailNow
(
t
,
err
)
throwFailNow
(
t
,
AssertIs
(
num
,
2
))
}
func
TestPrepareInsert
(
t
*
testing
.
T
)
{
qs
:=
dORM
.
QueryTable
(
"user"
)
i
,
err
:=
qs
.
PrepareInsert
()
...
...
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