# Relationships

For querying relationships, you can use QuerySeter or QueryM2Mer.

For example:

o := orm.NewOrm()
post := Post{Id: 1}
m2m := o.QueryM2M(&post, "Tags")
// In the first param object must have primary key
// The second param is the M2M field will work with
// API of QueryM2Mer will used to Post with id equals 1
1
2
3
4
5
6

Full API:

# QueryM2Mer Add

tag := &Tag{Name: "golang"}
o.Insert(tag)

num, err := m2m.Add(tag)
if err == nil {
	fmt.Println("Added nums: ", num)
}
1
2
3
4
5
6
7

Add accepts Tag,*Tag,[]*Tag,[]Tag,[]interface{}.

var tags []*Tag
...
// After reading tags
...
num, err := m2m.Add(tags)
if err == nil {
    fmt.Println("Added nums: ", num)
}
// It can pass multiple params
// m2m.Add(tag1, tag2, tag3)
1
2
3
4
5
6
7
8
9
10

# QueryM2Mer Remove

Remove tag from M2M relation:

Remove supports many types: Tag,*Tag,[]*Tag,[]Tag,[]interface{}.

var tags []*Tag
...
// After reading tags
...
num, err := m2m.Remove(tags)
if err == nil {
    fmt.Println("Removed nums: ", num)
}
// It can pass multiple params
// m2m.Remove(tag1, tag2, tag3)
1
2
3
4
5
6
7
8
9
10

# QueryM2Mer Exist

Test if Tag is in M2M relation:

if m2m.Exist(&Tag{Id: 2}) {
    fmt.Println("Tag Exist")
}
1
2
3

# QueryM2Mer Clear

Clear all M2M relation:

nums, err := m2m.Clear()
if err == nil {
    fmt.Println("Removed Tag Nums: ", nums)
}
1
2
3
4

# QueryM2Mer Count

Count the number of Tags:

nums, err := m2m.Count()
if err == nil {
    fmt.Println("Total Nums: ", nums)
}
1
2
3
4