Suppose I have the following data:
Table some_table
:
some_table_id | value | other_table_id
--------------------------------------
1 | foo | 1
2 | bar | 2
Table other_table
:
other_table_id | value
----------------------
1 | foo
2 | bar
Here, some_table
has a foreign key to column other_table_id
from other_table
into the column of some name.
With the following query in PostgreSQL:
SELECT *
FROM some_table
WHERE other_table_id = 3;
As you see, 3
does not exists in other_table
This query obviously will return 0 results.
Without doing a second query, is there a way to know if the foreign key that I am using as a filter effectively does not exist in the other_table
?
Ideally as an error that later could be parsed (as it happends when doing an INSERT
or an UPDATE
with a wrong foreign key, for example).