Using OR operator on columns of joined tables #2670
-
Environment
Description of issueIncluding a column of a joined table in an OR operator results in PostgREST parsing the column/table as operator, due to it strictly adhering to the schema of CREATE TABLE table1 (
id SERIAL PRIMARY KEY,
value1 INT,
value2 INT
);
CREATE TABLE table2 (
id SERIAL PRIMARY KEY,
value1 INT,
value2 INT
);
CREATE TABLE table3 (
id SERIAL PRIMARY KEY,
table1_id integer,
table2_id integer,
FOREIGN KEY (table1_id) REFERENCES table1 (id),
FOREIGN KEY (Table2_id) REFERENCES table2 (id)
); The SQL query I'm trying to replicate: SELECT id
FROM table3
JOIN table1 ON table1_id=table1.id
JOIN table2 ON table2_id=table2.id
WHERE table1.value1 > 1 OR table2.value2 > 5;
Is there a way to get the desired behaviour, short of creating a view? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
If I'm understanding correctly, you want to filter the result by the columns from the joined table, right? PostgREST allows this with ebedded filters. For your case it should be something like this: curl 'http://localhost:3000/table2?select=*,table1(*)&table1.or=(value1.gt.1,value2.gt.5)' If you want to filter the top level rows you need to add curl 'http://localhost:3000/table2?select=*,table1!inner(*)&table1.or=(value1.gt.1,value2.gt.5)' If this isn't what you meant, then add an SQL example of what you need to check it out. |
Beta Was this translation helpful? Give feedback.
This doesn't make sense.
The question is on which level you want to filter rows out? Do you want to filter child-level rows out, i.e. in the embedding? Then an
or
condition across multiple tables doesn't make sense.Do you want to filter top-level rows out? Then use what @steve-chavez suggested above: Make the …