{"html_url": "https://github.com/simonw/datasette/issues/356#issuecomment-495931140", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/356", "id": 495931140, "node_id": "MDEyOklzc3VlQ29tbWVudDQ5NTkzMTE0MA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2019-05-25T16:30:59Z", "updated_at": "2019-05-25T16:30:59Z", "author_association": "OWNER", "body": "I went with a much more simple URL scheme: `?_facet_m2m=destination_table` (it then figures out which the middle table is by looking at the foreign keys).\r\n\r\nThis can be extended to be more complicated in the future if needed.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 346028655, "label": "Ability to display facet counts for many-to-many relationships"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/356#issuecomment-495931078", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/356", "id": 495931078, "node_id": "MDEyOklzc3VlQ29tbWVudDQ5NTkzMTA3OA==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2019-05-25T16:30:09Z", "updated_at": "2019-05-25T16:30:09Z", "author_association": "OWNER", "body": "Implemented in https://github.com/simonw/datasette/commit/d923d847545e829bf946bb9170bebfc7c3f9d993\r\n\r\nDocumentation here: https://datasette.readthedocs.io/en/latest/facets.html#facet-by-many-to-many\r\n\r\nDemo here: https://latest.datasette.io/fixtures/roadside_attractions", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 346028655, "label": "Ability to display facet counts for many-to-many relationships"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/356#issuecomment-482620313", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/356", "id": 482620313, "node_id": "MDEyOklzc3VlQ29tbWVudDQ4MjYyMDMxMw==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2019-04-12T15:35:44Z", "updated_at": "2019-04-12T15:35:44Z", "author_association": "OWNER", "body": "One question here is how these facets should be defined in the table page query string.\r\n\r\n#427 started exploring this.\r\n\r\nFor any m2m facet we need to know:\r\n\r\n- what is the join table?\r\n- how is the join table related to our current table?\r\n- what is the table on the other side of the relationship?\r\n- how does that table relate to the join table?\r\n- how should that table be displayed (what's the label column)?\r\n\r\nThe simplest form of m2m relationship can be automatically derived from just knowing the table. We can support that like so:\r\n\r\n?_facet_m2m=tagged\r\n\r\nThis could work automatically if the following constraints turn out to apply:\r\n\r\n- the tagged table has a foreign key back to our table, against our primary key \r\n- the tagged table has a single other foreign key to one other table\r\n- that other table has a single text column we can use as the label (or has a label column defined in metadata)\r\n\r\nIf any of the above rules don't hold, I think the solution is to have explicit configuration. Per #427 this will likely be done using JSON in the query string. Something like this (would be one line but indented for readability):\r\n\r\n```\r\n?_facet_m2m={\r\n \"through\":\"tagged\",\r\n \"through_fk_us\":\"tree_id\",\r\n \"other\":\"tags\",\r\n \"through_fk_other\":\"tag_id\",\r\n \"other_label\": \"tag\"\r\n}\r\n```\r\n\r\nProbably also need a way of specifying the outbound column used on both us and other - if the m2m table isn't linking to the foreign keys.\r\n\r\nI don't yet like the names of the above keys.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 346028655, "label": "Ability to display facet counts for many-to-many relationships"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/356#issuecomment-409088967", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/356", "id": 409088967, "node_id": "MDEyOklzc3VlQ29tbWVudDQwOTA4ODk2Nw==", "user": {"value": 9599, "label": "simonw"}, "created_at": "2018-07-31T04:14:44Z", "updated_at": "2018-07-31T04:14:44Z", "author_association": "OWNER", "body": "Here's the query I'm playing with for facet counts:\r\n\r\nhttps://datasette-m2m-work-in-progress.now.sh/russian-ads-e8e09e2?sql=select+target_id%2C+count%28*%29+as+n+from+ad_targets%0D%0Awhere%0D%0A++target_id+not+in+%28%22ec3ac%22%2C+%22e128e%22%29%0D%0A++and+ad_id+in+%28select+ad_id+from+ad_targets+where+target_id+%3D+%22ec3ac%22%29%0D%0A++and+ad_id+in+%28select+ad_id+from+ad_targets+where+target_id+%3D+%22e128e%22%29%0D%0Agroup+by+target_id+order+by+n+desc%3B\r\n\r\n```\r\nselect target_id, count(*) as n from ad_targets\r\nwhere\r\n target_id not in (\"ec3ac\", \"e128e\")\r\n and ad_id in (select ad_id from ad_targets where target_id = \"ec3ac\")\r\n and ad_id in (select ad_id from ad_targets where target_id = \"e128e\")\r\ngroup by target_id order by n desc;\r\n```", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 346028655, "label": "Ability to display facet counts for many-to-many relationships"}, "performed_via_github_app": null}