How can I declare a relationship with a SELECT statement?
I have 2 tables,
iot_device
and iot_device_status
, where the status table saves all current and historical statuses of each device. I like to join the tables together as so:
I noticed relations
only take in a pgTable. How can I make it so that it takes the result of the given SQL statement instead?6 Replies
I don't think you can do that. I think what you want is actually a View
I could turn it into a view, but the relations field doesn't accept a view either
Why do you have to put it as relations?
I'm also interesting in inserting a view in a relation and I see it's not possible? Is there any workaround? Would it be implemented?
Because it helps tremendously with DX. The current status will always be needed in our app when we query iot device
I'm not home so I can't make one specifically to answer your question, but here's a snippet of code I already sent somewhere on Discord which can pretty trivially be changed to fit your needs:
Relations:
Note: as we've figured out today, this only works on many-to-x relations and you can only use the
where
clause on the many
side i.e. user -> messages
but not message -> sender