System Information
- Strapi Version: 4.3.2
- Operating System: Archlinux
- Database: Postgres 14.3
- Node Version: 16.16
- NPM Version: N/A
- Yarn Version: 1.22.19
I’ve noticed that when creating a many-to-many relationship through the content manager UI, it creates two tables, e.g. shoes_sizes_links and sizes_shoes_links. I am running a script that updates my shoe record via a REST api.
await fetch(`http://localhost:1337/api/shoes/1`, {
headers: {
Authorization: `Bearer ${TOKEN}`,
'Content-Type': 'application/json'
},
method: 'put',
body: JSON.stringify({
data: {
sizes: [4,5,6,7,7.5]
}
})
})
When this executes, it updates the sizes_shoes_links table (only).
On the querying side, I have tried both GraphQL and REST.
GraphQL:
This seems to not return the populated sizes.
query findByShoeId {
shoe(id: 1) {
data {
id
attributes {
sizes {
data {
attributes {
value
}
}
}
}
}
}
}
Returns
{
"data": {
"shoe": {
"data": {
"id": "1",
"attributes": {
"sizes": {
"data": []
}
}
}
}
}
}
REST:
http://localhost:1337/api/shoes/1?populate[sizes]=*
The REST version of the same query does seem to.
{
"data": {
"id": 1,
"attributes": {
"createdAt": "2022-08-01T17:56:48.092Z",
"updatedAt": "2022-08-02T03:45:59.712Z",
"publishedAt": "2022-08-01T17:56:48.090Z",
"sizes": {
"data": [
{
"id": 7,
"attributes": {
"value": "4",
"createdAt": "2022-08-01T17:21:35.478Z",
"updatedAt": "2022-08-01T17:21:35.478Z",
"publishedAt": null
}
},
{
"id": 9,
"attributes": {
"value": "5",
"createdAt": "2022-08-01T17:21:44.767Z",
"updatedAt": "2022-08-01T17:21:44.767Z",
"publishedAt": null
}
},
{
"id": 11,
"attributes": {
"value": "6",
"createdAt": "2022-08-01T17:21:57.027Z",
"updatedAt": "2022-08-01T17:21:57.027Z",
"publishedAt": null
}
},
{
"id": 13,
"attributes": {
"value": "7",
"createdAt": "2022-08-01T17:22:08.537Z",
"updatedAt": "2022-08-01T17:22:08.537Z",
"publishedAt": null
}
},
{
"id": 14,
"attributes": {
"value": "7.5",
"createdAt": "2022-08-01T17:22:13.635Z",
"updatedAt": "2022-08-01T17:22:13.635Z",
"publishedAt": null
}
}
]
}
}
However, this request http://localhost:1337/api/shoes?populate[sizes]=* does not.
{
"data": [
{
"id": 1,
"attributes": {
"createdAt": "2022-08-01T17:56:48.092Z",
"updatedAt": "2022-08-02T03:45:59.712Z",
"publishedAt": "2022-08-01T17:56:48.090Z",
"sizes": {
"data": []
}
}
}
],
...
}
In fact, I haven’t been able to put together any request GraphQL/REST that is able to fetch all shoes with populated sizes.
- Is the double join table generation intentional? If so, why?
- Is there some configuration in place to stop me from fetching nested many-to-many objects?
Thanks!