5

Im trying to show in a Datagrid a Field that is in a many to many relationship (intermediate table).

I have these tables:

group - group_subject - subject

  • A group can have many subjects
  • A subject can be connected with many Groups

group_subject also has attributes like rubrics and schedule

It's a many to many relationship. I would like to show in the <Resource> group all the subjects that a group is connected to.

Using something like the <SingleFieldList>: as this image

I'm using react-admin. Is there a way to get values in this relationship?

oskrgg
  • 101
  • 1
  • 9

2 Answers2

1

The easiest way would be to hide that intermediate table from react-admin. It's a good thing to hide this implementation detail anyway IMO. Users of your API shouldn't be aware of it.

  • from the groups resource perspective, a group has many subjects, each subject having a group_id and you API should allow to retrieve subjects by this group_id field

  • from the subjects resource perspective, a subject has many groups, each group having a subject_id and you API should allow to retrieve groups by this subject_id field

Gildas Garcia
  • 6,966
  • 3
  • 15
  • 29
  • I found on Github that `` and `` is intended for this use case, but I cant make them work. For example my REST API (OData) endpoint `User(1)/Roles` returns roles assigned under User with Id = 1. If I use those components I can specify `reference` and `target` props but I am not able to navigate React Admin to endpoint like `User(1)/Roles`. Users and Roles are in many-to-many relationship and I have pivot (intermediate) table to store relations for that, so I dont exactly have references in `User` or `Role` table. Any thoughts about this? – Allwe Jan 07 '19 at 17:39
  • Actually, even though your answer doesn't answer the question it is not a bad idea and it seems the best option, given 'react-admin' can't handle it. I just designed the backend dto to return results directly attached instead of the intermediary endpoint. Did the same for update to take directly IDs of end model. This way for the front end, no intermediate tables exist and works as expected with minimal beckend work. – mgPePe May 09 '20 at 08:50
  • 2
    react-admin does handle it, for a price: Their Enterprise support includes a ManyToMany component: https://marmelab.com/ra-enterprise/modules/ra-relationships#referencemanytomanyfield -- I was searching here to figure out if someone had created a free variant. No luck so far. – SomeCallMeTim Feb 18 '21 at 17:00
  • Have you made any progress in your search @SomeCallMeTim? It's weird to me that this is not something of a dealbreaker for using react admin in the first place. – Matthew Purdon Nov 02 '21 at 20:03
  • Sorry, that project ended up moving to a different platform and I didn't need to solve the problem at all any more. To be honest, though, paying for components shouldn't be considered an automatic deal-breaker. Developers gotta eat. – SomeCallMeTim Nov 10 '21 at 19:58
0

From react-admin documentation <ReferenceField> can be used for this purpose. More on documentation. Maybe this is a pretty late answer, but I think this can be helpful for others.

Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37
  • The free tools for react-admin only handle Many->One and One->Many. They've put the Many-to-Many support behind a paywall: https://marmelab.com/ra-enterprise/modules/ra-relationships#referencemanytomanyfield – SomeCallMeTim Feb 18 '21 at 16:58