how i can link two table like this
right now i got this sql query
im trying to replicate in xata but its like too difficult i already did some of them except LEFT JOIN variation_location_details as vld
ON vld.variation_id = v.id this one not sure how to do it if you see the attachment is the original variation_location_details tables and theres a product id that should be matched in product table attached too
9 Replies
with the others i dont have issue, but with this yes because for each product could be 1 or more records (this is how my current inventory system works, it add each transaction as a new records and i need to sum up all qty so i can show like productA actualstock = 10 units for example or productB actual stock = 20
whats the best approach for this? using rest for sure
This reads like a one-to-many relationship between each record in the products table, with potentially many records in the variation_location_details table.
If you'd like to use Xata's REST API (without resulting to SQL statements) you can use a combination of links and the backwards reference operator (https://xata.io/docs/concepts/data-model#links-and-relationships)
In practice this means: the product_id colum in variation_location_details instead of a regular string or int, would be a "link" column to the products table.
You can then query the products table to get a summary count of the number of references to each of its records, from the variation_location_details table.
Here's an example query and sample result output:
That said, Xata's REST API needs the link column type to establish relationships between records. You'd need to make sure a valid product xata_id value is set in the link field "product" of variation_location_details.
The alternative is to default to pure SQL, where the link column type is irrelevant as relations can be established on any column with joins.
How i can do that but using api rest? With json
You can run this REST API call which will return the raw record data, and then calculate the counts at the client side out of the response.
It will return the
variation_location_details
records that link to each of the products, groupped in array under each product. It's just a matter of picking the length of that array, that is the count you're looking for per product.
i got this
in theory i should have count of product 1 = 2 and product_id 2 = 2 also
got it then i just need to count the array for each id, works good, thank you very much
Unfortunately the REST API does not support summarizing on records returned from reverse link lookups so you can fall back to SQL (over HTTP) for that part.
Alternatively, you can do the summary calculations at the client side, after retrieving all the records with the <- operator.