Since I just ran into this issue, here’s a very simple, not super efficient but working for the moment solution… It’s for tags, but should work for all kinds of relations. It’s of course not great that every row gets re-inserted - one could write some logic to only write rows that don’t exist yet. But hey, on a small site, let’s just keep it simple…
this is just using the lifecyle function beforeUpdate to wipe the relations and re-write them in the new order, before updating the rest of the document.
module.exports = {
lifecycles: {
async beforeUpdate(params, data) {
if (params.id) {
if (data.tags) {
await strapi.connections.default.raw(
`DELETE FROM projects_tags__tags_projects WHERE project_id=${params.id};`
);
for (const tag of data.tags) {
await strapi.connections.default.raw(
`INSERT INTO projects_tags__tags_projects (project_id, tag_id) VALUES (${params.id}, ${tag});`
);
}
}
}
},
},
};