Could not find a relationship between 'messages' and 'user' in the schema cache

I'm using the js client to try to attach a "user" to each "message". You can see from the attached screenshot that I have the user id setup as a fk. I believe this is all correct. I have two RLS policies on messages:
alter policy "Enable all actions for authenticated users only"
on "public"."messages"
to authenticated
using (
true
with check (
true
);

alter policy "check either user or agent is not null"
on "public"."messages"
to public
using (
((author_user_uuid IS NOT NULL) OR (author_agent_uuid IS NOT NULL))
with check (
((author_user_uuid IS NOT NULL) OR (author_agent_uuid IS NOT NULL))
);
alter policy "Enable all actions for authenticated users only"
on "public"."messages"
to authenticated
using (
true
with check (
true
);

alter policy "check either user or agent is not null"
on "public"."messages"
to public
using (
((author_user_uuid IS NOT NULL) OR (author_agent_uuid IS NOT NULL))
with check (
((author_user_uuid IS NOT NULL) OR (author_agent_uuid IS NOT NULL))
);
I don't have any user table policies because those are locked (by design I think?) Now, I'm getting the data like this
const { data, error } = await this.#client
.from("messages")
.select(`
*,
agent:author_agent_uuid(uuid, display_name),
user(id, email)
`)
.eq("channel_uuid", channelId)
.order("created_at", { ascending: true });
const { data, error } = await this.#client
.from("messages")
.select(`
*,
agent:author_agent_uuid(uuid, display_name),
user(id, email)
`)
.eq("channel_uuid", channelId)
.order("created_at", { ascending: true });
And I thought that this would give me the user as { ...message, user }. But it's always returning null. The messages have all been created with a valid user. How can I debug this further?
No description
5 Replies
garyaustin
garyaustin9mo ago
You cannot access auth schema from the API. Do you have a public user table?
AndyJessop
AndyJessopOP9mo ago
Ah. No, I don't have a public users table. What's the normal pattern for this? Create a corresponding users table and then sync them? I feel this would be quite a common thing to try and do, so maybe there are docs/articles on it?
garyaustin
garyaustin9mo ago
Yes.
AndyJessop
AndyJessopOP9mo ago
Oh nice, that's perfect! Thank you very much 🙏

Did you find this page helpful?