Smart relationship: action to add an existing relationship triggers the same route to list relationship

Hello there,

I created a smart relationship (hasMany) between two different collections. It’s working fine I can load the relationship data and it’s being shown in the Related Data section.
I’m facing one problem when I try to add a new item to the relationship created.
These are the steps:

  1. I click on the plus button
  2. I select that I want to add an existing relationship
  3. I search for the record I want to create the relationship
  4. I press the Add button

The result is that when I press the Add button the API call being sent to the Express Server is the same that is sent when the relationship data is loaded:

  • it’s a GET request without any information about the relationship I want to create

For example this is the route that I defined to load the relationship data:

router.get('/templates/:template_id/relationships/categories' In my case the relationship is between templates and categories. I can load the data and is working fine.

When I click the button to add a new item to this relationship the same route above is triggered and I am confused why this is happening. Shouldn’t it be a POST request? Is this expected? If so how I am supposed to differentiate between listing records and creating relationships?

Hi @nathanqueija,

Can you please share the forest models involved in this relationship definition?

Thank you

Here they are. They are both smart collections:

collection('templates', {
  actions: [
    {
      name: 'Upload Assets',
      type: 'single',
      fields: [
        {
          field: 'Poster',
          description: 'Preview image',
          type: 'File',
        },
        {
          field: 'Video',
          description: 'Video preview',
          type: 'File',
        },
      ],
    },
  ],
  fields: [
    { field: 'id', type: 'String' },
    { field: 'name', type: 'String' },
    { field: 'description', type: 'String' },
    { field: 'creator', type: 'String' },
    { field: 'assetKey', type: 'String' },
    { field: 'slides', type: 'String' },
    { field: 'poster', type: 'String' },
    { field: 'video', type: 'String' },
    { field: 'publishOn', type: 'Date' },
    { field: 'createdAt', type: 'Date' },
    { field: 'updatedAt', type: 'Date' },
    { field: 'categories', type: ['String'], reference: 'templates_categories.id' },
  ],
  segments: [],
})

collection('templates_categories', {
  fields: [
    { field: 'id', type: 'String' },
    { field: 'title', type: 'String' },
    { field: 'description', type: 'String' },
  ],
  segments: [],
})

Thank you.

I am asking my teammates if the feature is supported.

Don’t know if this is related but I already had a problem with smart collections calling wrong routes previously. I tracked it here: Smart Action load hook - endpoints 404 error - #10 by lamatt_v

Thank you.

At this time it seems we do not support that yet.
I am creating a product insight ticket (the effect is to request for this feature to be implemented)

Regards

There is an alternative and I explain it below, but I suggest to just use the native forms to create records for each standard relationship instead.

It’s possible to create a smart action. The smart action should have as input the ids required to store the relationship.
But as said previously, it’s coding something that is already supported natively.

@Sliman_Medini what do you mean by native forms? We need to have this as a smart collection because we fetch the data from an external API. Would it be possible to achieve the same result?

The smart action suggestion doesn’t work for what we need, unfortunately.

Ok,
To give better advice, can you please describe in more detail what you need?

Regards

We don’t expect that the users will know the IDs or type any data to create the relation. We wanted the user to open a Template and select which categories they want to add or remove in this relationship. It means that is not about providing a field to type ids. We want to show which categories already exist in this relationship. I’d consider the smart action if I could return a multiple selection dropdown where the users could select or remove items so we can process this operation in the backend. Something similar to what you provide for the relationship UI if it worked for this use case.

I could use the load hook to return the categories that are present in the relationship, but I’m struggling to understand what I can do to provide the user a UI to add or remove categories from this relationship.

By the way, the post route for the relationships doesn’t seem to work even for native forms. I tried to implement it for another collection that is using sequelize and when I click on the plus button to add a relationship the GET route is called in the backend as it’s happening for the smart collections.
I couldn’t find any examples in the documentation (Create a Smart relationship - Documentation) for the POST route, it only shows an example for getting the relationship data, but not for creating a relation. Does it mean the smart relationships only work for GET requests and we’re not able to create relationships through smart relationships?

Could you please share a video and show us the networks tab open, there should be a POST not a GET call :thinking:

@vince I sent you a DM with a video showing this problem happening

@Sliman_Medini @vince I understand what’s happening. It looks like this feature is indeed supported. What is happening is that if the user tries to add a relationship that already exists ForestAdmin will not make the POST call to the express server and only the GET call to get the list of relationships attached.
I tried to add a relationship that didn’t exist yet, i. e., that was not returned in the GET call and the POST call is sent, even the DELETE call is working properly so I think that this is not an issue.
I’d appreciate it if this could be in the docs. It took me two days between trying to understand if this was supported or if I was doing something wrong.
If the documentation is open source I can even edit it and add some examples. Lemme know.
Thanks.

Well, now it started working for existing relationships as well :man_shrugging:t3: don’t know if you are changing something, but the POST call is now being sent as expected for any relationship I try to add.

1 Like