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

**URL:** <https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986>\
**Category:** ORM\
**Created:** [September 19, 2023, 3:44pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986 "2023-09-19T15:44:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [September 19, 2023, 3:44pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/1 "2023-09-19T15:44:28Z")

</div>

Hi,

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](https://forum.djangoproject.com/t/2-ideas-new-additions-to-makemigrations/2654) – 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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 19, 2023, 4:24pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/2 "2023-09-19T16:24:45Z")

</div>

> [@shaib](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [September 21, 2023, 4:54am UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/3 "2023-09-21T04:54:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [October 10, 2023, 3:33pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/4 "2023-10-10T15:33:54Z")

</div>

The big issue I see with “migration bankruptcy” is that it doesn’t include custom SQL or other operations from the deleted migrations files. That might mean failing to apply some specific changes not yet supported by Django, like custom data types.

> [@shaib](#):
>
> 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.

If we could teach `squashmigrations` to work on mutliple apps, it could convert circular relationships into multiple migrations in one app. Then maybe we wouldn’t need `--no-deps` ?

---

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [October 10, 2023, 5:52pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/5 "2023-10-10T17:52:11Z")

</div>

> [@adamchainz](#):
>
> If we could teach `squashmigrations` to work on mutliple apps, it could convert circular relationships into multiple migrations in one app. Then maybe we wouldn’t need `--no-deps` ?

The current implementation of `squashmigrations` is:

- Find a list of all the migrations which should be squashed
- Collect operations from all of them, essentially, into one big migration
- Unless told otherwise, optimize this migration

What you’re suggesting is definitely better than `--no-deps`, but requires a complete rewrite. Current code isn’t even graceful about dependencies – as far as I can tell, it will gladly include more than one dependency on the same app, and has no capacity to break the squashed migrations to more than one (and it is possible for it to “know” that it needs to – e.g. if migration 0002 has

```auto
run_before=[('other_app', '0017')]

```

and migration 0003 has

```auto
depends=[
    ('our_app', '0002'),
    ('other_app', '0017'),
]

```

Then there will be need to be a break between migrations up to 0002 and migrations starting at 0003 – but `squashmigrations` doesn’t even look at that.

`--no-deps`, on the other hand, seems like it can be implemented within reasonable effort (ignore all deps except those of the first migration, ignore all operations to create or modify relation fields), and still be useful – and relatively safe, as it avoids the problems you mention (correctly) with bankruptcy.

---

<div class="post-metadata">

**Author:** ![duck-nukem](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duck-nukem/32/19362_2.png) [@duck-nukem](https://forum.djangoproject.com/u/duck-nukem)\
**Post date:** [February 22, 2024, 11:16am UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/6 "2024-02-22T11:16:17Z")

</div>

I previously had done something similar (if I understand the original request correctly), I wrote two scripts for squashing “safe” migrations (no dependencies to other apps, no data migrations via runSQL or runPytohn). Is this something you’re looking for?

Squashing: [squash\_migrations.py · GitHub](https://gist.github.com/duck-nukem/1e2b4fc177039eb0c5f8c8c3fc0e4c95)  
Then removing squashed migrations after they’ve been applied: [remove\_squashed\_migrations.py · GitHub](https://gist.github.com/duck-nukem/384f4b9d85787ff46ddf17e2a5d67a08)

If it is something along the line of what you were thinking, would it be something that could be contributed to Django? We though of this as a quick win, as it could often reduce the number of migrations significantly, but it’s not perfect.

---

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [June 8, 2024, 12:49pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/7 "2024-06-08T12:49:39Z")

</div>

Sorry for the delayed reply – no, your `squash_migrations.py` is not what I’m looking for; IIUC, it squashes when it knows things are fine, otherwise leaves things for later. What I want is to _ignore_ dependencies – do the squash even when dependencies should, technically, get in the way, with the assumption that the developer doing this knows what they’re doing and will make sure to fix the issues.

---

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [June 9, 2024, 7:56am UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/8 "2024-06-09T07:56:33Z")

</div>

[https://code.djangoproject.com/ticket/35508](https://code.djangoproject.com/ticket/35508)

---

<div class="post-metadata">

**Author:** ![David-Wobrock](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/david-wobrock/32/21906_2.png) [@David-Wobrock](https://forum.djangoproject.com/u/David-Wobrock)\
**Post date:** [June 15, 2024, 7:41am UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/9 "2024-06-15T07:41:46Z")

</div>

Hey,

Thanks for the idea!

> with the assumption that the developer doing this knows what they’re doing and will make sure to fix the issues.

This is a part that bothers me. This option wouldn’t _just work_ for users, which is the preferred way of doing things in Django IMO, but rather requires a more advanced understanding of migrations.

I rather concur with @adamchainz, that making `squashmigrations` able to generate more than one file in order to resolve circular dependencies would be a sweet feature addition.  
Of course, it’s more effort - but perhaps it’s more worth it compared to adding an option that is meant to be deleted 🙂

---

<div class="post-metadata">

**Author:** ![shaib](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shaib/32/35_2.png) [@shaib](https://forum.djangoproject.com/u/shaib)\
**Post date:** [June 16, 2024, 8:22pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/10 "2024-06-16T20:22:21Z")

</div>

> [@David-Wobrock](#):
>
> This option wouldn’t _just work_ for users, which is the preferred way of doing things in Django IMO, but rather requires a more advanced understanding of migrations.

Migrations, in general, were never like that – `makemigrations` has always worked for the simple cases, but required editing when things get hairier. This is in the same department.

> [@David-Wobrock](#):
>
> making `squashmigrations` able to generate more than one file in order to resolve circular dependencies would be […] more worth it compared to adding an option that is meant to be deleted

For us to say that the option is meant to be deleted, IMO, we need to have at least a clear plan for adding the replacement. As far as I can tell, the algorithms are not known yet, and even the requirements are not well-defined.

Further, I have a hunch that in some cases, `--ignore-deps` can solve problems that just separation to break cycles will not; I think one such example is an app being removed, where operations between the addition of an FK to it, and the removal of that FK, prevent these two operations from being reduced to nothing.

---

<div class="post-metadata">

**Author:** ![GitRon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gitron/32/13088_2.png) [@GitRon](https://forum.djangoproject.com/u/GitRon)\
**Post date:** [June 19, 2024, 11:47am UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/11 "2024-06-19T11:47:55Z")

</div>

Interesting topic! I talked to some folk on the DjangoCon in Vigo about my [django-migration-zero](https://django-migration-zero.readthedocs.io/en/latest/) package/approach which is basically the idea of the “migration bankruptcy” (nice term!) because squashing is super hard AND has in my and a bunch of other people zero benefit if you control all the databases (mostly for applications), compared to a package for example.

So, bottomline: A big +1 for having a migration clean-up command that “just works” in the case that you don’t need a history…

---

<div class="post-metadata">

**Author:** ![tolomea](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tolomea/32/976_2.png) [@tolomea](https://forum.djangoproject.com/u/tolomea)\
**Post date:** [April 22, 2025, 4:00pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986/12 "2025-04-22T16:00:20Z")

</div>

> [@shaib](#):
>
> Migrations, in general, were never like that – `makemigrations` has always worked for the simple cases, but required editing when things get hairier. This is in the same department.

My issue with this is with squashing those hairier cases often require a lot of work.

Also makemigrations is handling the majority of stuff automatically these days, I’ve got to be doing something really weird to trip it up, like moving models between apps.

For context our main project is over 5 years old, it has 2 dozen apps and collectively they’ve had one and a half thousand migrations. There are a lot of cross app FK’s.  
Fortunately our situation is simplified because we never use RunSQL and RunPython is only used for updating existing data never for creating data.  
Also we’re still on 4.2.

For us massaging a squash typically took days of work, which also meant we were only doing it every year or so.

Also aside from circular dependency issues my experiences with the optimiser have been lack lustre and compared to bankruptcy optimising seems like a fundamentally complex approach. A thing that will always need more work and never be perfect.

Because of the above difficulties we have given up on the built in squash functionally.

Instead we’ve adopted a process that’s kinda a hybrid between bankruptcy and the multiple squash file approach @adamchainz suggested.  
We call this “crushing” and it is working surprisingly well for us.

The guts of it is when we squash we overwrite the initial migrations on each app with all the current table creation statements and create a squash on top of that which adds all the foreign keys.

This is “safe” because as long as there’s no really new apps you aren’t going to run one of those two and not the other.  
If you are updating then the initial will already be marked as applied and some (if not all) the migrations in the squash will be applied so it can’t be used either.  
If you are creating from fresh it will always use the initial and the squash.  
So the migrations just need to get to the right state by the end of the squash it doesn’t matter what crimes you commit in between.

We have all this automated in a script, but it’s not usable as a general process, it has stuff specific to our code base and it does things that are quite dirty.

This is a “what’s working for us” post not a “here’s a patch” post.

If anyone is curious you can find the script here [crush\_migrations/crush\_migrations.py at main · tolomea/crush\_migrations · GitHub](https://github.com/tolomea/crush_migrations/blob/main/crush_migrations.py)

I ran this this morning and it worked perfectly, took me maybe half an hour to “squash” a couple of hundred migrations. I’ve quite literally spent longer writing this message.
