Check fields, names, and conditions of indexes.
(self, model, connection)
| 84 | return bool(self.expressions) |
| 85 | |
| 86 | def check(self, model, connection): |
| 87 | """Check fields, names, and conditions of indexes.""" |
| 88 | errors = [] |
| 89 | # Index name can't start with an underscore or a number (restricted |
| 90 | # for cross-database compatibility with Oracle) |
| 91 | if self.name[0] == "_" or self.name[0].isdigit(): |
| 92 | errors.append( |
| 93 | checks.Error( |
| 94 | "The index name '%s' cannot start with an underscore " |
| 95 | "or a number." % self.name, |
| 96 | obj=model, |
| 97 | id="models.E033", |
| 98 | ), |
| 99 | ) |
| 100 | if len(self.name) > self.max_name_length: |
| 101 | errors.append( |
| 102 | checks.Error( |
| 103 | "The index name '%s' cannot be longer than %d " |
| 104 | "characters." % (self.name, self.max_name_length), |
| 105 | obj=model, |
| 106 | id="models.E034", |
| 107 | ), |
| 108 | ) |
| 109 | references = set() |
| 110 | if self.contains_expressions: |
| 111 | for expression in self.expressions: |
| 112 | references.update( |
| 113 | ref[0] for ref in model._get_expr_references(expression) |
| 114 | ) |
| 115 | errors.extend( |
| 116 | model._check_local_fields( |
| 117 | { |
| 118 | *[field for field, _ in self.fields_orders], |
| 119 | *self.include, |
| 120 | *references, |
| 121 | }, |
| 122 | "indexes", |
| 123 | ) |
| 124 | ) |
| 125 | # Database-feature checks: |
| 126 | required_db_features = model._meta.required_db_features |
| 127 | if not ( |
| 128 | connection.features.supports_partial_indexes |
| 129 | or "supports_partial_indexes" in required_db_features |
| 130 | ) and any(self.condition is not None for index in model._meta.indexes): |
| 131 | errors.append( |
| 132 | checks.Warning( |
| 133 | "%s does not support indexes with conditions." |
| 134 | % connection.display_name, |
| 135 | hint=( |
| 136 | "Conditions will be ignored. Silence this warning " |
| 137 | "if you don't care about it." |
| 138 | ), |
| 139 | obj=model, |
| 140 | id="models.W037", |
| 141 | ) |
| 142 | ) |
| 143 | if not ( |
nothing calls this directly
no test coverage detected