| 1329 | |
| 1330 | def test_unique_constraint_pointing_to_missing_field(self): |
| 1331 | class Model(models.Model): |
| 1332 | class Meta: |
| 1333 | constraints = [models.UniqueConstraint(fields=['missing_field'], name='name')] |
| 1334 | |
| 1335 | self.assertEqual(Model.check(), [ |
| 1336 | Error( |
| 1337 | "'constraints' refers to the nonexistent field 'missing_field'.", |
| 1338 | obj=Model, |
| 1339 | id='models.E012', |
| 1340 | ), |
| 1341 | ]) |
| 1342 | |
| 1343 | def test_unique_constraint_pointing_to_m2m_field(self): |
| 1344 | class Model(models.Model): |
| 1345 | m2m = models.ManyToManyField('self') |
| 1346 | |
| 1347 | class Meta: |
| 1348 | constraints = [models.UniqueConstraint(fields=['m2m'], name='name')] |
| 1349 | |
| 1350 | self.assertEqual(Model.check(), [ |
| 1351 | Error( |
| 1352 | "'constraints' refers to a ManyToManyField 'm2m', but " |
| 1353 | "ManyToManyFields are not permitted in 'constraints'.", |
| 1354 | obj=Model, |
| 1355 | id='models.E013', |
| 1356 | ), |
| 1357 | ]) |
| 1358 | |
| 1359 | def test_pointing_to_non_local_field(self): |
| 1360 | class Foo(models.Model): |
| 1361 | field1 = models.IntegerField() |
| 1362 | |
| 1363 | class Bar(Foo): |
| 1364 | field2 = models.IntegerField() |
| 1365 | |
| 1366 | class Meta: |
| 1367 | constraints = [models.UniqueConstraint(fields=['field2', 'field1'], name='name')] |
| 1368 | |
| 1369 | self.assertEqual(Bar.check(), [ |
| 1370 | Error( |
| 1371 | "'constraints' refers to field 'field1' which is not local to " |
| 1372 | "model 'Bar'.", |
| 1373 | hint='This issue may be caused by multi-table inheritance.', |
| 1374 | obj=Bar, |
| 1375 | id='models.E016', |
| 1376 | ), |
| 1377 | ]) |
| 1378 | |
| 1379 | def test_pointing_to_fk(self): |
| 1380 | class Foo(models.Model): |
| 1381 | pass |
| 1382 | |
| 1383 | class Bar(models.Model): |
| 1384 | foo_1 = models.ForeignKey(Foo, on_delete=models.CASCADE, related_name='bar_1') |
| 1385 | foo_2 = models.ForeignKey(Foo, on_delete=models.CASCADE, related_name='bar_2') |
| 1386 | |
| 1387 | class Meta: |
| 1388 | constraints = [models.UniqueConstraint(fields=['foo_1_id', 'foo_2'], name='name')] |
| 1389 | |
| 1390 | self.assertEqual(Bar.check(), []) |