Repository: django/django
File: tests/invalid_models_tests/test_models.py (Python)

Code before:
                    obj=ReferenceTab,
                    id="models.E049",
                ),
            ],
        )


@isolate_apps("invalid_models_tests")
class IndexesTests(TestCase):
    def test_pointing_to_missing_field(self):
        class Model(models.Model):
            class Meta:
                indexes = [models.Index(fields=["missing_field"], name="name")]

        self.assertEqual(
            Model.check(databases=self.databases),
            [
                Error(
                    "'indexes' refers to the nonexistent field 'missing_field'.",
                    obj=Model,
                    id="models.E012",
                ),
            ],
        )

    def test_pointing_to_m2m_field(self):
        class Model(models.Model):
            m2m = models.ManyToManyField("self")

            class Meta:
                indexes = [models.Index(fields=["m2m"], name="name")]

        self.assertEqual(
            Model.check(databases=self.databases),
            [
                Error(
                    "'indexes' refers to a ManyToManyField 'm2m', but "
                    "ManyToManyFields are not permitted in 'indexes'.",
                    obj=Model,
                    id="models.E013",
                ),
            ],
        )

    def test_pointing_to_non_local_field(self):
        class Foo(models.Model):
            field1 = models.IntegerField()

        class Bar(Foo):
            field2 = models.IntegerField()


Review comment (suggestion):
```suggestion
        self.assertEqual(ReferenceTab.check(), [])

    def test_pointing_to_foreign_object_multi_column(self):
        class Reference(models.Model):
            reference_id = models.IntegerField(unique=True)
            code = models.CharField(max_length=1)

        class ReferenceTabMultiple(models.Model):
            reference_id = models.IntegerField()
            code = models.CharField(max_length=1)
            reference = models.ForeignObject(
                Reference,
                from_fields=["reference_id", "code"],
                to_fields=["reference_id", "code"],
                on_delete=models.CASCADE,
            )

            class Meta:
                unique_together = [["reference"]]

        self.assertEqual(
            ReferenceTabMultiple.check(),
            [
                Error(
                    "'unique_together' refers to a ForeignObject 'reference' "
                    "with multiple target fields, which is not supported.",
                    obj=ReferenceTabMultiple,
                    id="models.E049",
                ),
            ],
        )

```

Code after:

        class ReferenceTabMultiple(models.Model):
            reference_id = models.IntegerField()
            code = models.CharField(max_length=1)
            reference = models.ForeignObject(
                Reference,
                from_fields=["reference_id", "code"],
                to_fields=["reference_id", "code"],
                on_delete=models.CASCADE,
            )

            class Meta:
                unique_together = [["reference"]]

        self.assertEqual(
            ReferenceTabMultiple.check(),
            [
                Error(
                    "'unique_together' refers to a ForeignObject 'reference' with "
                    "multiple 'from_fields', which is not supported for that option.",
                    obj=ReferenceTabMultiple,
                    id="models.E049",
                ),
            ],
        )


@isolate_apps("invalid_models_tests")
class IndexesTests(TestCase):
    def test_pointing_to_missing_field(self):
        class Model(models.Model):
            class Meta:
                indexes = [models.Index(fields=["missing_field"], name="name")]

        self.assertEqual(
            Model.check(databases=self.databases),
            [
                Error(
                    "'indexes' refers to the nonexistent field 'missing_field'.",
                    obj=Model,
                    id="models.E012",
                ),
            ],
        )

    def test_pointing_to_desc_field(self):
        class Model(models.Model):
            name = models.CharField(max_length=100)

            class Meta:
                indexes = [models.Index(fields=["-name"], name="index_name")]