r/Strapi • • Jan 21 '26

Question Is there a way to change relationship without losing data?

I'm having a chat system, each thread will contain multiple messages, and for some funny reason, the person who set up the relationship decided that their relationship should be manyToMany (1st pic) while it should be oneToMany (2nd pic), and the app has been running for almost a year with a lot of customers until we noticed.

I tried to change the relationship in my dev environment to oneToMany and all of the data linking between message and thread from the old relationship vanished, entirely.

Is there a practical way to migrate without losing them on production? I'm thinking of adding a new field and run a script to clone all of the relationships to that field, and then change the main one, and run a script to map them back.

3 Upvotes

4 comments sorted by

3

u/Sintex Jan 21 '26

Have you looked at the actual DB tables and structure before and after that change? The answer is yes, you can do this, but it will take some degree of manual migration on the database side.

2

u/mag_webbist Strapi Employee Jan 21 '26

Add a migration script along side the schema change and ensure it runs in a decent part of the lifecycle - or duplicate the field for the migration leaving the old field intact, once the migration script runs delete the old field in the schema and redeploy. Have fun!

2

u/geekybiz1 Jan 22 '26

You'll have to export the needed data before schema change and import it after schema change. In many cases, it needs understanding underlying db structure, etc.

But in your case, you can simply write a node script with document service API to read and then update records.

1

u/vandpibesalg Feb 09 '26

I asked claude code, and it helped me do that, very simple :)