I have a `users_pages` many-to-many table. The use...
# off-topic
l
I have a
users_pages
many-to-many table. The users in this table are stored by
user_id
foreign key. I'm trying to display all of the
email addresses
, for every user that belongs to a given
page
. So I need to find all of the `user_id`s for a requested page — and then pull the
email addresses
associated with each of those `user_id`s. Something like this:
Copy code
javascript
        const { data, error } = await supabase
        .from('users_pages')
        .select(`*, users(email)`)
        .eq('page_id', PAGE_ID)
What query should go into
.select()
, in order to return the
email addresses
from
users
table?
s
Hm, this should work:
Copy code
js
        const { data, error } = await supabase
        .from('pages')
        .select(`*, users(email)`)
        .eq('id', PAGE_ID)
Then you'd have a response like:
Copy code
json
[
  {"id": 1, "users": [
    {"email": "a@user.com"}
  ]}
]
l
@User thanks so much for the input on this. When I tried with that syntax, it returned:
Copy code
{message: "Could not find a relationship between pages and users in the schema cache",…}
hint: "If a new foreign key between these entities was created in the database, try reloading the schema cache."
I've attached an image of how my public`users_pages` table is setup. The only other tables in the DB are the standard
auth.users
table, and the public
pages
(which is indexed by
id
, as in your example.) Does it looked set-up correctly? Any other thoughts?
Also, any input on how to 'reload the schema cache', if that's relevant?
Update, the issue was that I was trying to query an
auth.users
table instead of a
public.users
table. When I created the
public.users
table and ran the same set-up, this worked!
Copy code
javascript

        const { data, error } = await supabase
        .from('pages')
        .select(`*, users(email)`)
        .eq('id', this_page.id)