{"action":"created","issue":{"url":"https://api.github.com/repos/go-pg/pg/issues/297","repository_url":"https://api.github.com/repos/go-pg/pg","labels_url":"https://api.github.com/repos/go-pg/pg/issues/297/labels{/name}","comments_url":"https://api.github.com/repos/go-pg/pg/issues/297/comments","events_url":"https://api.github.com/repos/go-pg/pg/issues/297/events","html_url":"https://github.com/go-pg/pg/issues/297","id":172366650,"number":297,"title":"Adding where clause to ManyToMany to filter on join table","user":{"login":"sinclairday","id":431789,"avatar_url":"https://avatars.githubusercontent.com/u/431789?v=3","gravatar_id":"","url":"https://api.github.com/users/sinclairday","html_url":"https://github.com/sinclairday","followers_url":"https://api.github.com/users/sinclairday/followers","following_url":"https://api.github.com/users/sinclairday/following{/other_user}","gists_url":"https://api.github.com/users/sinclairday/gists{/gist_id}","starred_url":"https://api.github.com/users/sinclairday/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sinclairday/subscriptions","organizations_url":"https://api.github.com/users/sinclairday/orgs","repos_url":"https://api.github.com/users/sinclairday/repos","events_url":"https://api.github.com/users/sinclairday/events{/privacy}","received_events_url":"https://api.github.com/users/sinclairday/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-08-22T04:14:50Z","updated_at":"2016-08-24T04:31:52Z","closed_at":"2016-08-22T08:12:46Z","body":"I'm trying to filter out records in a M2M relationship and can't figure out how to structure the query.\r\n\r\nWhat's the best way to filter out Items whose Count in the join table is 1? \r\n\r\n```\r\ntype List struct {\r\n    Id         int\r\n    Name       string\r\n    CreatedAt  time.Time\r\n    Items []Item `pg:\",many2many:list_items\"`\r\n}\r\n\r\ntype ListItem struct {\r\n    ListId            int    `sql:\",pk\"` // pk tag is used to mark field as primary key\r\n    Name              string `sql:\",pk\"`\r\n    Count             int\r\n}\r\n\r\ntype Item struct {\r\n\tName        string `sql:\",pk\"`\r\n}\r\n\r\n// select a list by its name and filter items whose count = 1\r\nvar l List\r\nerr := r.db.Model(&l).\r\n    Column(\"list.*\", \"Items\").\r\n    Relation(\"Items\", func(q *orm.Query) *orm.Query {\r\n        return q.WhereOr(pg.SQL(\"count = ?\", 1))\r\n    }).\r\n    Where(\"list.name = ?\", listName).\r\n    Select()\r\nreturn l, err\r\n```"},"comment":{"url":"https://api.github.com/repos/go-pg/pg/issues/comments/241954682","html_url":"https://github.com/go-pg/pg/issues/297#issuecomment-241954682","issue_url":"https://api.github.com/repos/go-pg/pg/issues/297","id":241954682,"user":{"login":"sinclairday","id":431789,"avatar_url":"https://avatars.githubusercontent.com/u/431789?v=3","gravatar_id":"","url":"https://api.github.com/users/sinclairday","html_url":"https://github.com/sinclairday","followers_url":"https://api.github.com/users/sinclairday/followers","following_url":"https://api.github.com/users/sinclairday/following{/other_user}","gists_url":"https://api.github.com/users/sinclairday/gists{/gist_id}","starred_url":"https://api.github.com/users/sinclairday/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sinclairday/subscriptions","organizations_url":"https://api.github.com/users/sinclairday/orgs","repos_url":"https://api.github.com/users/sinclairday/repos","events_url":"https://api.github.com/users/sinclairday/events{/privacy}","received_events_url":"https://api.github.com/users/sinclairday/received_events","type":"User","site_admin":false},"created_at":"2016-08-24T04:31:52Z","updated_at":"2016-08-24T04:31:52Z","body":"Thanks, and thanks for the speedy update! That worked like a charm."}}
{"actor":{"display_login":"sinclairday"}}