I have multiple postgres functions that takes in a number of arguments in order to write on multiple tables.
fn_trade
(
_product_id BIGINT[],
_user_id BIGINT[],
_location_id BIGINT
)
Since the arguments comes from the client or a Node.js server, I need to validate that the data exists (handled by FK constraints, but not in the case of BIGINT[]), and I need confirm that the arguments' relationships are valid. For example, I have to check that the user
and product
exists for the location
, and the product
belongs to the user
. The complexity of this check grows with more arguments. I may end up with more than 10 arguments, and checking their relationships will be complicated and costly.
I don't know if I am missing a crucial picture by trying to validate function arguments. Is this the right approach? Is there a better way to have confidence in the argument's relationships so that the populated data will always be expected?
A possible approach might be to predict the result and check if the result is written once the write is done (else rollback)?
Postgres 9.6+
Edit: Added an example to clarify the question
Tables:
location (id, name) -- e.g. 1, 'Canada'
user (id, name, location_id) -- e.g. 1, Jane, 1
product (id, name, user_id, location_id) -- e.g. 1, 'skates', 1, 1
trade (id, user_id[], product_id[]) -- e.g. 1, [1,2], [1,2,3,4]
When a trade happens I need to confirm that the data given is matches the data within the database.
For example, I get:
fn_trade
(
[1,2,3,4],
[1,2],
1
)
So the trade happens for users [1,2] for products [1,2,3,4] in Canada. Users can have items in different locations and users can move to different locations.
When inserting user_id[]
and product_id[]
into trade
, the check for individual ids are not automatic. I don't know if its possible to set the values within an array as FKs, so the check here is manual. I need to guarantee ids [1,2,3,4] all exists for the product.
But what about relations? I need to check that the ids also belongs to the users [1,2].
Belonging to the user is not enough. The products should exist in the same location where the trade is happening.
And for the users, I need to know if its possible for them to trade. If they live in separate locations (Canada v.s. Taiwan), then the trade cannot happen.
Since client data relies on the DB, and data passed to fn_trade
might be unreliable, how should the relation be validated to prove that the data is true for our user, products, location relations?