# Accessing custom route for smart relationships

**URL:** <https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819>\
**Category:** Help me!\
**Tags:** related-data, smart-relationship\
**Created:** [January 11, 2023, 9:26am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819 "2023-01-11T09:26:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 11, 2023, 9:26am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/1 "2023-01-11T09:26:19Z")

</div>

I am trying to create a route using RAW SQL for Smart-Relationships. As shown in the guide/documentation.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/0/09cd6e48544d46740c6075376f81f7788653507e.png)  
The route written in this code, I need to access it. But the URL scheme on the forest admin dashboard is quite different from what is written in the route function.  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9d9e7fed48eb4acf0a6202038127c100d8805df9.png)  
Can anyone help me understand how I access the route I have written in my route function? Please note that I have deployed my application before testing it on Heroku.

---

<div class="post-metadata">

**Author:** ![nielswhydonate](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/nielswhydonate/32/5520_2.png) [@nielswhydonate](https://community.forestadmin.com/u/nielswhydonate)\
**Post date:** [January 11, 2023, 2:11pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/2 "2023-01-11T14:11:07Z")

</div>

I would also be very interested in this answer. Does anyone have any suggestions for this?

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [January 11, 2023, 4:02pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/3 "2023-01-11T16:02:40Z")

</div>

Hello @Sehban_Alam and welcome to our community!

The smart relationship feature allows us to create a link between two collections. As shown on [the documentation](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/create-a-smart-relationship), this will add a “Related data” section when visualising a record from a collection.  
Aside from defining the route, you have to [declare the field](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/create-a-smart-relationship#creating-a-hasmany-smart-relationship) for the collection with the entity it references.

On the exemple you have posted, you would be able to reach the endpoint by going to the “products” collection, click on a record and on the “Summary” tab you will be able to see your relationships under the “Related data” section with a “buyers” elements which will call this route.

I hope this helps.

Best regards,

Dogan

---

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 12, 2023, 8:55am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/4 "2023-01-12T08:55:48Z")

</div>

Thank you for reply on this. I still have few queries. What you have guided me through is about a single field only, In my case, I have a table that is related to another table,  
Table 1 - User Records  
Table 2 - User Bank Information  
How do I show this information because this is not a single entity?  
Also, Table 1 is related to 10 other tables, each table has almost 8 fields. does that mean I have to add 80 fields to show the data?

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [January 12, 2023, 10:06am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/5 "2023-01-12T10:06:02Z")

</div>

The relationship is made between two collections. As such, the elements you would find under the “Related data” section are collections. If you wish to create a smart relationship between your Table 1 and the 10 other tables you would need to declare 10 fields on the collection of your Table 1. Each of those fields would reference the id of one of the 10 tables. You will then be able to find those collections under the “Related data” section when visualising a record from Table 1.

If you look closely to the code you have posted as exemple, the result sent at the end is a list of customers with all the fields, not a single field of the customer table.

---

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 12, 2023, 11:07am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/6 "2023-01-12T11:07:01Z")

</div>

Thank you so much for your response. I will try this one.

---

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 16, 2023, 1:15pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/7 "2023-01-16T13:15:20Z")

</div>

@anon7284711  
Hi,

So I have a table called users and another one OPP. These two tables do not have any relationship on DB level. But each row of OPP table contains “user\_id” which belongs to “id” of user table.  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/0/0ade98e184869cfe02cb7c7651e173456a1d1989.png)  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/e/edda322178430324d2d707acff0f9aaad7cdc327.png)

I am stuck again and my best guess I am missing some code. So Far I have

1. Added a field in “forest/auth-user.js”  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/a9ca51c4d7c074c1e34388156f6c67c540c98550.png)
2. Added a route as shown in documentation for RAW SQL. (documentation: [Create a Smart relationship - Developer guide](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/create-a-smart-relationship))  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/c/c819e686cca1f36671e092cb561a8cfd9142dc30.png)
3. When I am trying to access the URL: “[https://app.forestadmin.com/whydonate-admin/Staging/Operations/data/authUser/index/record/authUser/43422/relationships/opp](https://app.forestadmin.com/whydonate-admin/Staging/Operations/data/authUser/index/record/authUser/43422/relationships/opp)”  
it throws error.  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/0/0b88d34b6bb7a00abb3bc2b55b4cac160684eb6b.png)  
Please guide me what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [January 16, 2023, 3:38pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/8 "2023-01-16T15:38:08Z")

</div>

Hey,

On your first step when defining the fields, be careful to use camelcase for the field name (use: “oppData”). On the reference, you should define “oppData.id”, instead of “user\_id”.

On the second step, when implementing the route. You should serialize the result of your query as shown on the documentation.

I hope this helps.

Best regards,

Dogan

---

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 16, 2023, 5:55pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/9 "2023-01-16T17:55:18Z")

</div>

Thank you for your quick response.  
I have updated the code as suggested by you.  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/1/10e7279b98e0b36bec6116b1ae12b0998e586682.png)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/0/02bcdbb2ea6d4cd1c526b2ec58f82f3cc8696971.png)

But I am still getting the same error

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/5/5ade8267b8d261eebbd8cba7834d8fd5b4326d08.png)

Just to make sure. This Data Exist in our DB

→ opp Table

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/f/f8deb7543f8333b737f7f06e578837561a1e9dcf.png)

→ User Table

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/1/1ea94ce1a3762484e9a49123667436e6f08b359a.png)

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [January 17, 2023, 9:42am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/10 "2023-01-17T09:42:41Z")

</div>

I didn’t take notice of it until now, but it seems you are manually editing the url to query the route. Smart relationships are defined on an already existing collection and you access them after opening the details of a record from said collection. In your case you would need to go to your “Users” collection, click on a record, in the “Summary” or “Details” tab you would have your smart relationships listed under “Related data”.

 ![smart relationship](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/ba5b85a081ca2b1462d9344aca7b73f96a4d926d.png)

---

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 17, 2023, 11:12am UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/11 "2023-01-17T11:12:40Z")

</div>

@anon7284711  
Thank you for your response.  
I can not see the related data in my collection.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/79322c0c38f59ae46f956b0a95315915268d6668.png)

Also, when I try to edit it, The “Related Data” tab is greyed out.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/3/3d510f43c5bbb078cc1706cc956b37c9268e3b93.png)

I do not understand what am I doing wrong here.

I am sharing the “models” code as well just in case I have made any mistake in the model.  
 → Users Table

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/c/c6b38801425233742e0eb7d26e665e15068ac95e.png)

→ OPP Table

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/f/fae6161fb951f8ccdba61ceed56423ebfefbc186.png)

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [January 17, 2023, 1:59pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/12 "2023-01-17T13:59:33Z")

</div>

I have made a simple project with similar tables for your use case. This was done with mysql and an agent type express-sequelize.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/493198fc49e88aa1649f29f14b9b0616b508c699.png)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/8b0c81f0de4efed52c9f966276c17db2de423877.png)  
There is no relation between the tables in the db level.

## In order are the steps and implementation I have made to create a smart relationship:

- In `forest/auth-user.js`  

- In `routes/auth-user.js`  

- In `forest/opp-table.js`  

You have to define a model for the opp-table for the serializer. This is a necessity since we’re using a raw SQL query and not passing through Sequelize’s models.  
While implementing this I noticed that the documentation was not up to date and I am sorry that it hindered you. We are going to update the documentation.

I have created a repository with this project on [my github](https://github.com/DayTF/smart-relationship), you should be able to access it.

Best regards,

Dogan

---

<div class="post-metadata">

**Author:** ![Sehban\_Alam](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sehban_alam/32/5596_2.png) [@Sehban\_Alam](https://community.forestadmin.com/u/Sehban_Alam)\
**Post date:** [January 17, 2023, 3:03pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/13 "2023-01-17T15:03:26Z")

</div>

@anon7284711  
Thank you so much for it. This will help a lot. Kindly share the GitHub Repo link as well.

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [January 17, 2023, 3:14pm UTC](https://community.forestadmin.com/t/accessing-custom-route-for-smart-relationships/5819/14 "2023-01-17T15:14:42Z")

</div>

Sorry forgot to add it on my previous comment ^^', [here it is](https://github.com/DayTF/smart-relationship) aswell.
