| 528 | def test_func_index_complex_expression_custom_lookup(self): |
| 529 | from django.db.models.functions import Abs |
| 530 | from django.test.utils import register_lookup |
| 531 | |
| 532 | class Model(models.Model): |
| 533 | height = models.IntegerField() |
| 534 | weight = models.IntegerField() |
| 535 | class Meta: |
| 536 | indexes = [ |
| 537 | models.Index( |
| 538 | models.F('height') / (models.F('weight__abs') + models.Value(5)), |
| 539 | name='name', |
| 540 | ), |
| 541 | ] |
| 542 | |
| 543 | with register_lookup(models.IntegerField, Abs): |
| 544 | self.assertEqual(Model.check(), []) |
| 545 | |
| 546 | def test_func_index_pointing_to_missing_field(self): |
| 547 | class Model(models.Model): |
| 548 | height = models.IntegerField() |
| 549 | weight = models.IntegerField() |
| 550 | class Meta: |
| 551 | indexes = [models.Index(Lower('missing_field').desc(), name='name')] |
| 552 | |
| 553 | self.assertEqual(Model.check(), [ |
| 554 | Error( |
| 555 | "'indexes' refers to the nonexistent field 'missing_field'.", |
| 556 | obj=Model, |
| 557 | id='models.E012', |
| 558 | ), |
| 559 | ]) |
| 560 | |
| 561 | def test_func_index_pointing_to_m2m_field(self): |
| 562 | class Model(models.Model): |
| 563 | m2m = models.ManyToManyField('self') |
| 564 | |
| 565 | class Meta: |
| 566 | indexes = [models.Index(Lower('m2m'), name='name')] |
| 567 | |
| 568 | self.assertEqual(Model.check(), [ |
| 569 | Error( |
| 570 | "'indexes' refers to a ManyToManyField 'm2m', but " |
| 571 | "ManyToManyFields are not permitted in 'indexes'.", |
| 572 | obj=Model, |
| 573 | id='models.E013', |
| 574 | ), |
| 575 | ]) |
| 576 | |
| 577 | def test_func_index_pointing_to_non_local_field(self): |
| 578 | class Foo(models.Model): |
| 579 | field1 = models.CharField(max_length=15) |
| 580 | |
| 581 | class Bar(Foo): |
| 582 | class Meta: |
| 583 | indexes = [models.Index(Lower('field1'), name='name')] |
| 584 | |
| 585 | self.assertEqual(Bar.check(), [ |
| 586 | Error( |
| 587 | "'indexes' refers to field 'field1' which is not local to " |
| 588 | "model 'Bar'.", |
| 589 | hint='This issue may be caused by multi-table inheritance.', |
| 590 | obj=Bar, |
| 591 | id='models.E016', |
| 592 | ), |
| 593 | ]) |
| 594 | |
| 595 | def test_func_index_pointing_to_fk(self): |
| 596 | class Foo(models.Model): |
| 597 | pass |
| 598 | |
| 599 | class Bar(models.Model): |
| 600 | foo_1 = models.ForeignKey(Foo, models.CASCADE, related_name='bar_1') |
| 601 | foo_2 = models.ForeignKey(Foo, models.CASCADE, related_name='bar_2') |
| 602 | |
| 603 | class Meta: |
| 604 | indexes = [ |
| 605 | models.Index(Lower('foo_1_id'), Lower('foo_2'), name='index_name'), |
| 606 | ] |
| 607 | |
| 608 | self.assertEqual(Bar.check(), []) |
| 609 | |