I'm building a REST API and server using Django and Django Rest Framework. We are using a postgres database.
I need to simplify a badly designed relation. We have a model (House
) that has a ManyToMany
relationship to another (City
). In reality it would be enough when this is a ForeignKey
relationship.
I googled and couldn't find any blog posts or docs how to properly migrate in this direction. I could only find the other way (FK to M2M).
I'm 98% sure that all the data on the server will be consistent with a FK relationship (meaning I'm pretty sure all houses only have one city). We need to change the relationship for several reasons and aren't able to keep the M2M.
I'm afraid to just change the model and running makemigrations
and migrate
. I was wondering, how do you properly migrate from M2M to FK? Are there any caveats I have to take into account? How can I deal with data if surprisingly there are houses with multiple city's? The data set is still quite small (less than 10k entries) if that matters.
Thank you very much.