Related data not working

Feature(s) impacted

I have a user model which has an organization through user.organizationId property.

View on related data is impacted. On the user details I can see that the user is linked to an Organization

Observed behavior

I can see the user has an organization through the user details view

If I click on related Organizations related data on the left instead, no related data is found :

If I go on Ouicool organization detail view, and I click on the related users data, I can see no users. Also, I can see two times users related data, whereas we should only see one entry for users related data on the left.

Expected behavior

  • When I click on the Organizations related data on the left, I should see an entry with Ouicool organization, as the user detail view shows.
  • When I go on the Ouicool organization detail view, I should be able to see which users are linked to this organization

Failure Logs

No failure logs

Context

  • Project name: Lift - Web App
  • Team name: Lift
  • Environment name: staging
  • Database type: PostgreSQL
  • Recent changes made on your end if any: none

And, if you are self-hosting your agent:

  • Agent technology: nodejs
  • Agent (forest package) name & version (from your .lock file): @forestadmin/agent@1.64.0

Hi @Mattia and welcome to our community !

Did you add a relationship through the agent code on the Organizations collection ?

Regards,

Shohan

Hi @shohanr !

Thanks for your reply :slight_smile:
I did not specify any relations on the agent code indeed.

I defined the one-to-many relationship twice so :

  • one for the Organization table
  • one for the User table

and I can now correctly list the organizations for a certain user on Forest.

But this was working before, without need of declaring anything in the agent code, and the relationships between the other tables are still working even if not defined in the agent.

Do I need to create all the relationships among my tables in Forest Agent code now ?

Is the following way of declaring the one-to-many relationship (a user can belong to only one organization, an organization has multiple users) correct ?

agent.customizeCollection('User', (collection) => {
  collection.addOneToManyRelation('organizations', 'Organization', {
    originKey: 'id',
    originKeyTarget: 'organizationId'
  });
});

agent.customizeCollection('Organization', (collection) => {
  collection.addOneToManyRelation('users', 'User', {
    originKey: 'organizationId',
    originKeyTarget: 'id'
  });

Best regards,

Hi @Mattia !

Base relationships are automatically created like before, you should not have to manually declare most of them.
One-to-many relationships are displayed as a field: organization should be a field of your user, leading to the organization record. So it’s not displayed in the related data list.
However, as organizations may have several users, you should see “Users” in the related data of any organization.

Your declarations of one-to-many relationships are not correct:
You should use addManyToOneRelation to create the user to organizations relationship:

collection.addManyToOneRelation('organizations', 'organization', { foreignKey: 'organizationId' });

Best regards

Thanks for the reply @Enki !

  • I guess I should declare that on the User collection, right ?

  • What other relationships need to be declared ?

I think I mixed up things a little bit by the way (sorry ! But still, your replies helped a lot already !)

So let’s say (many-to-many) :

  • a user can belong to several organizations
  • an organization can thus have several users

how should that be declared ?

Thanks in advance !

First, you need to have a database structure to allow that, an organizationId in the user table will not be enough: https://stackoverflow.com/questions/51463706/can-somebody-give-a-practical-example-of-a-many-to-many-relationship

Then, Forest Admin will use the relationship table to create the relationship. If not (that may be because the naming of your table or attributes is not standard), you can use the addManyToManyRelation customization to create it.

Don’t be afraid to ask chatGPT for help in first instance, for such things, it can help you a lot.

Best regards