Hi!
So i’ve 2 collections: Team and Rollen with in Rollen the fields id and naam.
Now i’ve made a Many-to-Many relationship between Team and Rollenwith as key Rol. It shows the junction as Team_Rollen.
In Access Policies → Publici have allowed Team, Rollen and Team_Rollen to be publicly accessible.
I query it with: /items/Team tho i only get the role ID of that team:
{
"id": 1,
"avatar": "199bc1ca-8c96-4b9f-b836-bdb4a6f1a087",
"naam": "HeapReaper",
"Rol": [
1
]
},
I did figgle around with ?fields= tho i can’t seem to figure it out.
Help is appriciated!
I think this might be two separate things happening:
1) Expanding the many-to-many relation
For a M2M field like Rol, Directus returns only the IDs unless you tell it to expand the related items. Try a query like:
/items/Team?fields=*,Rol.*
or, if your junction uses something like Rollen_id, then:
/items/Team?fields=*,Rol.Rollen_id.*
(Just replace the field name with whatever you see inside the junction.)
2) Permissions / Access Policy
If you’re calling the API with a token, Directus will not use the Public role.
Access policies for Public only apply when the request is made without any token.
So if the token’s role doesn’t have access to Rollen or Team_Rollen, the API will only show IDs.
Try either:
- Removing the Authorization header to test
Public access, or
- Giving the authenticated role permissions for
Rollen and the junction table.
Ahmad has provided the correct answers for the fields parameter. Some helpful resources to review in our docs:
Regarding M2M relationships:
- fields=
* —> returns all fields in the current collection (relational fields will be foreign key value)
- fields=
m2m_field.* –> returns all junction table fields (junction PK, FK current collection, FK far collection, if any exist also includes direct fields in the junction table)
- fields=
m2m_field.far_table_fk.* –> returns all fields in the far collection
Hope this helps!