Closed pcardosolei closed 1 year ago
Detecting views foreign keys is a best effort right now - they follow the rules on https://postgrest.org/en/stable/api.html#embedding-views.
CREATE OR REPLACE VIEW example_view AS
SELECT
childs.id AS child_id,
to_tsvector(
'english', concat_ws(' ', parents.name, childs.name)) AS fts
FROM
childs
JOIN parents ON childs.parent_id = parents.id;
The child_id
from the view above comes from the childs
table(childs.id
) and on the next snippet
export const searchText = async (text: string) => { const query = supabase.from("example_view").select(", child:childs()"); const { data, error } = await query; return { data, error }; }; This will fail.
You do supabase.from("example_view").select(", child:childs()")
, which try to embeds childs
(where example_view
comes from) with childs
, since there's no FK relationship between childs
and childs
the detection will fail.
It is worth noting that I didn't manage to get successful results while querying both the tables when both id are in the view.
Try including childs.id
, childs.parents_id
and parents.parents_id
in your view for this to work.
Q: If you're doing the JOIN inside the view already, why do you need to do the join client-side? For this case it'd be simpler to have it all on the view.
Detecting views foreign keys is a best effort right now - they follow the rules on https://postgrest.org/en/stable/api.html#embedding-views.
CREATE OR REPLACE VIEW example_view AS SELECT childs.id AS child_id, to_tsvector( 'english', concat_ws(' ', parents.name, childs.name)) AS fts FROM childs JOIN parents ON childs.parent_id = parents.id;
The
child_id
from the view above comes from thechilds
table(childs.id
) and on the next snippetexport const searchText = async (text: string) => { const query = supabase.from("exampleview").select(", child:childs(_)"); const { data, error } = await query; return { data, error }; }; This will fail.
You do
supabase.from("example_view").select(", child:childs()")
, which try to embedschilds
(whereexample_view
comes from) withchilds
, since there's no FK relationship betweenchilds
andchilds
the detection will fail.It is worth noting that I didn't manage to get successful results while querying both the tables when both id are in the view.
Try including
childs.id
,childs.parents_id
andparents.parents_id
in your view for this to work.
Makes sense. I was doing some extra tests + reading the documentation and I thought about it but was not 100% sure.
Thanks.
Q: If you're doing the JOIN inside the view already, why do you need to do the join client-side? For this case it'd be simpler to have it all on the view.
I tried to do an approach to push the full text search out of the childs table because I tried to do some sort of combined FTS with combination of columns for the child table + parent table where most of the information came from the child table but a few important would also come from the parent table.
Need to think about a new way to do this then. Maybe doing a query to get the FTS results and then another to get the information from the desired table.
Thanks.
Bug report
Describe the bug
select() fails in certain situations to recognize to id of tables. The following situation is between a view and 2 tables to explain the situation.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
Starting by adding the following script to the sql table.
Query for the supabase-js:
This will fail.
Expected behavior
I will show cases that will fail and cases that will not fail. Looking at the case to reproduce. The following select will fail to get the join of table and the view.
Looking at the SQL preview:
Results in
Now. If i had the foreign_key in the view as the result of the select it will work.
querying the parents table will work.
querying both will fail even thought he manages to get the parents.
Another example:
This will make the parents also fail.
but childs relationship starts working.
It is worth noting that I didn't manage to get successful results while querying both the tables when both id are in the view.
I tried different ways like parents!inner or parent!variable but with no success.
Screenshots
Added on the expected behavior.
System information
Additional context
Add any other context about the problem here.