# Related Tables Query Filter

**URL:** <https://community.dreamfactory.com/t/related-tables-query-filter/3949>\
**Category:** uncategorized\
**Created:** [July 13, 2017, 1:29pm UTC](https://community.dreamfactory.com/t/related-tables-query-filter/3949 "2017-07-13T13:29:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hussain\_Fakhruddin](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/hussain_fakhruddin/32/935_2.png) [@Hussain\_Fakhruddin](https://community.dreamfactory.com/u/Hussain_Fakhruddin)\
**Post date:** [July 13, 2017, 1:29pm UTC](https://community.dreamfactory.com/t/related-tables-query-filter/3949/1 "2017-07-13T13:29:38Z")

</div>

Hi, How can I filter something based on a related table.

For example, I need to join on a condition.

[Parent.id=child.id](http://Parent.id=child.id) = This is possible.

But what about extra clause and [child.name](http://child.name) like “%ABC%”

This is a major setback for us, because we have hundreds of records and currently its retrieving everything based on FK relationships.

I know there is a way to write a custom script. But this is very common, so I was hoping it could be in core DF itself.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![mattschaer](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mattschaer/32/914_2.png) [@mattschaer](https://community.dreamfactory.com/u/mattschaer)\
**Post date:** [July 19, 2017, 3:03pm UTC](https://community.dreamfactory.com/t/related-tables-query-filter/3949/2 "2017-07-19T15:03:23Z")

</div>

@Hussain_Fakhruddin There is a feature request filed to support this which has yet to be implemented. To filter on related data currently a custom script is required.Running something like this as post-process would probably work  
`var relationship_name = ''; // The name of the relationship we want to filter var related_filter_name = ''; // The name of the URL parameter that contains the related filter var related_service_name = ''; // The name of the service that contains the related data var related_table_name = ''; // The name of the table that contains the related data var related_id_field = ''; // The id field in the related table var related_endpoint = related_service_name + '/_table/' + related_table_name; var options = {}; options.parameters = {}; var params = event.request.parameters; if (params.hasOwnProperty('related')) { if (params.hasOwnProperty(related_filter_name)) { if (params.related.hasOwnProperty(relationship_name)) { options.parameters.fields = related_id_field; options.parameters.filter = params[related_filter_name]; var related_result = platform.api.get(related_endpoint, null, options); var data = related_result.content; if (data.error) throw data.error.message; if (!data.resource) throw 'No records in response'; var ids = data.resource.map(function(record) {return record[related_id_field];}); event.response.content.resource.forEach(function(parent_record){ parent_record[relationship_name] = parent_record[relationship_name].filter(function(obj){ return (ids.indexOf(obj.id) >= 0); }); }); } } }`

---

<div class="post-metadata">

**Author:** ![Hussain\_Fakhruddin](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/hussain_fakhruddin/32/935_2.png) [@Hussain\_Fakhruddin](https://community.dreamfactory.com/u/Hussain_Fakhruddin)\
**Post date:** [July 20, 2017, 9:14am UTC](https://community.dreamfactory.com/t/related-tables-query-filter/3949/3 "2017-07-20T09:14:46Z")

</div>

Hi @mattschaer Thanks for your reply.

Looks like this could work.

However, we have already made enhancement at our end to handle this and multiple sub table query filters from the API itself. Its working fine.

If needed we can submit our enhancement to the main key owners of the open source project as our contribution.

Kind Regards,  
Hussain

---

<div class="post-metadata">

**Author:** ![mattschaer](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mattschaer/32/914_2.png) [@mattschaer](https://community.dreamfactory.com/u/mattschaer)\
**Post date:** [July 20, 2017, 11:49am UTC](https://community.dreamfactory.com/t/related-tables-query-filter/3949/4 "2017-07-20T11:49:04Z")

</div>

@Hussain_Fakhruddin you can submit a pull request on GitHub. [DreamFactory](https://github.com/dreamfactorysoftware/dreamfactory) follows standard Git [guidelines for contributions](https://wiki.dreamfactory.com/DreamFactory/Contribution_Guide).
