              [
                  migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]),
                  migrations.CreateModel("LinkThrough", []),
                  migrations.AddField(
                      "Foo", "link", models.ManyToManyField("migrations.Link", through="migrations.LinkThrough")
                  ),
              ],
          )
  
      def test_create_model_alter_field(self):
 
</s> remove         # Note: The middle model is not actually a valid through model,
        # but that doesn't matter, as we never render it.
 </s> add  </s> remove                 migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]),
                migrations.CreateModel("LinkThrough", []),
 </s> add                 migrations.CreateModel('Employee', []),
                migrations.CreateModel('Employer', []),
                migrations.CreateModel('Employment', [
                    ('employee', models.ForeignKey('migrations.Employee', models.CASCADE)),
                    ('employment', models.ForeignKey('migrations.Employer', models.CASCADE)),
                ]),
                    'Employer', 'employees', models.ManyToManyField(
                        'migrations.Employee', through='migrations.Employment',
                    )
              [
                  migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]),
                  migrations.CreateModel("LinkThrough", []),
                  migrations.AddField(
                    'Employer', 'employees', models.ManyToManyField(
                        'migrations.Employee', through='migrations.Employment',
                    )
                  ),
              ],
          )
  
      def test_create_model_alter_field(self):
keep keep keep keep replace keep keep keep keep keep
 Refs #22875 -- Fixed an optimizer test to use a valid scenario.

An explicit intermediary many-to-many relationship must declare forward and
reverse foreign keys. The original issue was in the autodetector as these
operations shouldn't have been generated in this order in the first place
which is tested by AutodetectorTests.test_create_with_through_model. 
tests/migrations/test_optimizer.py