Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to display facet counts for many-to-many relationships #356

Closed
simonw opened this issue Jul 31, 2018 · 4 comments
Closed

Ability to display facet counts for many-to-many relationships #356

simonw opened this issue Jul 31, 2018 · 4 comments
Labels

Comments

@simonw
Copy link
Owner

simonw commented Jul 31, 2018

Parent: #354

@simonw
Copy link
Owner Author

simonw commented Jul 31, 2018

Here's the query I'm playing with for facet counts:

https://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

select target_id, count(*) as n from ad_targets
where
  target_id not in ("ec3ac", "e128e")
  and ad_id in (select ad_id from ad_targets where target_id = "ec3ac")
  and ad_id in (select ad_id from ad_targets where target_id = "e128e")
group by target_id order by n desc;

@simonw
Copy link
Owner Author

simonw commented Apr 12, 2019

One question here is how these facets should be defined in the table page query string.

#427 started exploring this.

For any m2m facet we need to know:

  • what is the join table?
  • how is the join table related to our current table?
  • what is the table on the other side of the relationship?
  • how does that table relate to the join table?
  • how should that table be displayed (what's the label column)?

The simplest form of m2m relationship can be automatically derived from just knowing the table. We can support that like so:

?_facet_m2m=tagged

This could work automatically if the following constraints turn out to apply:

  • the tagged table has a foreign key back to our table, against our primary key
  • the tagged table has a single other foreign key to one other table
  • that other table has a single text column we can use as the label (or has a label column defined in metadata)

If 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):

?_facet_m2m={
  "through":"tagged",
  "through_fk_us":"tree_id",
  "other":"tags",
  "through_fk_other":"tag_id",
  "other_label": "tag"
}

Probably 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.

I don't yet like the names of the above keys.

@simonw
Copy link
Owner Author

simonw commented May 25, 2019

@simonw simonw closed this as completed May 25, 2019
@simonw
Copy link
Owner Author

simonw commented May 25, 2019

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).

This can be extended to be more complicated in the future if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant