How to filter a table using a single-relation field? (something to the effect of: select * from posts where user_id = 123 )

I have a user table. (fields: name, email)
I have a post table. (fields: title, user, content)

Post table has a relationship field to pick a single user.

Using API, how do I get all the posts belong to a certain user. I played around filters

filters = [{"field_id": "584491","field_type": "single_relation","match_type": "exact","values": [{"value": user_id}],"type": "single_relation"}]

But I can’t find documentation on how to filter via a single relation field.

Any help is appreciated.

It is quite late here so i could be wrong but i think the easiest way is to flip the problem on its head and use:

POST
https://api.tapeapp.com/v1/record/app/{app_id}/ref/{ref_app_id}

this way you can get all the posts related to a user

2 Likes

To expand on this if you want the posts relating to an individual the individual is your ‘known’ point so you start there.

Here I have two apps (Individuals and Related data):

If we look in the ‘Fred’ record we can see that he is related to 3 records via an incoming relation:

Now the call looks like:

  1. is the individuals app number as that is our starting point
  2. Is the Posts (related data) app
  3. is the individuals record ID
  4. is the direction of relation so in this case reverse

If you do want or need to go from the post as the starting point then you just reverse things in the call so:

  1. becomes the posts app
  2. the individual app number
  3. the post record ID
  4. the direction is now forward

And the result is a single record of ‘Fred’

Hopefully that makes sense and helps

3 Likes

Thank you, @Jason . That works well. Very helpful. I am able to get the related items. Is there a way we could sort it as well?

My end goal is to get that last related item created. Currently its sorted ascending. Could be sort descending by created at?

1 Like

I don’t think you can pre-sort those you would need to sort/filter the results post retrieval they will include the creation date but I guess depending on what you are bringing them into will depend on how easy that is to do.

The other way especially if you only want the latest would be to run an automation in Tape maybe just add the latest posts record Id to a number field then you could get the person record retrieve the number field value and then get that record which might be easier again depending on your system.

Yeah, filtering after won’t work as we just wouldn’t know how many records to get.

An automation might be the way. Every time new relation is created, the user record (number field) can be updated with latest post. I just have to grab that then.

Thanks a ton, Jason. I could make that work.

1 Like