What I'm trying to do is to have a table that assigns a subscription to a user. All the samples I found so far do this via:
(auth.uid() = user_id)
However I would like to keep track of reassignments. With a table structured like this:
// assigments //
user_id | subscription_id | comment | created_at
---------------------------------------------------
3 | 1001 | re-assign | 2023-06-06
---------------------------------------------------
2 | 1002 | intial | 2023-06-05
---------------------------------------------------
1 | 1001 | intial | 2023-06-05
---------------------------------------------------
I'm having a difficult time wrapping my head around writing policy for this. I think it needs to be something like
auth.uid() IN (SELECT DISTINCT ON (subscription_id) user_id FROM assignments ORDER BY created_at)
Is this the right track? auth.uid() IN (...) feels super inefficient. Is it possible to first see if there is a row that matches user_id == auth.uid() and to a subquery on the resulting subscription_id with a ORDER BY created_at, LIMIT 1 and somehow use the result for the auth.uid() comparison?