Idea: {make,squash}migrations --no-deps


I’ve been thinking about the problem of large projects with many models and migrations, and circular cross-app dependencies. In such projects, squashmigrations typically fails, and in many cases, one cannot even “declare migration bankruptcy” – the practice where you just delete all migrations from the code and the DB and start afresh: you can’t make the initial migration for app A because it depends on (i.e. has a model with an FK to a model in) app B, which depends on app C, which depends app A.

This is a different take on a problem which was tackled in 2 Ideas: New additions to makemigrations – the idea there was a somewhat dangerous operation to replace a whole set of migrations with new initial ones.

I’m suggesting that we add a --no-deps flag to makemigrations and squashmigrations. In makemigrations, this will create the models without the relationships; in squashmigrations, I’m not entirely sure, but in general, try to get from the state before the first squashed migration to the state after the last squashed ones, again, without creating relationships and dependencies. The user will then be able to run makemigrations for each (or all) of the apps, to create the relationships.

This will support the case not handled in the previous suggestion, where the FK dependencies are circular, preventing the creation of the new migrations. squashmigrations can be more careful about the RunSQL and RunPython (and other) operations.

This is just an initial thought; comments (including pointing out why I’m wrong and this can’t work or shouldn’t be supported) welcome.

Actually, this works in the cases I have tried. Migrations picks one model and creates it without the FK field, creates the other models in turn, then adds the FK field to the original model.

1 Like

This had not been my experience, but last time I tried was a few years ago. So I went back to a project where I ran into this, which has a couple dozen apps and a few thousands of migrations, and tried again, and indeed, “migration bankruptcy” works much better than I remembered.

But squashmigrations only works on one app at a time, so telling it to ignore external dependencies while it does that may still be helpful IMO.