Accessing custom route for smart relationships

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


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
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.

1 Like

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

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, 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 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

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?

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.

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

@dogan.ay
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
image

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
  2. Added a route as shown in documentation for RAW SQL. (documentation: Create a Smart relationship - Developer guide)
  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”
    it throws error.

    Please guide me what am I doing wrong?

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

Thank you for your quick response.
I have updated the code as suggested by you.
image

But I am still getting the same error

Just to make sure. This Data Exist in our DB

→ opp Table

→ User Table

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”.

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

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

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

→ OPP Table

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


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:

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, you should be able to access it.

Best regards,

Dogan

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

Sorry forgot to add it on my previous comment ^^', here it is aswell.