Opened 6 years ago

Closed 5 years ago

#13016 closed Bug (duplicate)

Invalid ForeignKey ids in fixtures do not cause any error messages

Reported by: Art <artem.skvira@…> Owned by: nobody
Component: Testing framework Version: 1.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When referencing a non-existing entity instance in fixtures for ForeignKey relationship no error message is thrown in test runner mode (./ test). Please note below how in fixtures instance of Parent contains invalid ID for Child object (2 instead of 1).

This seems to be related to "always opened transaction" natures of tests, because if I run ./ testserver data.json I actually get an exception thrown.

Backend is Postgresql 8.3.


  5 class Parent(models.Model):
  6     child = models.ForeignKey('Child')
  8 class Child(models.Model):
  9     name = models.CharField(default='some name', max_length=100)

Fixture (data.json):

352     {
353         "pk": 1,
354         "model": "testapp.parent",
355         "fields": {
356             "child": 2
357         }
358     },
359     {
360         "pk": 1,
361         "model": "testapp.child",
362         "fields": {
363             "name": "child"
364         }
365     }


  5 class SimpleTest(TestCase):
  7     fixtures = ['data.json']
  9     def test_data(self):
 10         self.assertEqual(len(models.Parent.objects.all()), 1)
 11         self.assertEqual(len(models.Child.objects.all()), 1)

Change History (5)

comment:1 Changed 6 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by kmike

I found current behaviour very useful because it means I'm not obligated to write fixtures for all relations if I don't want to. Sometimes there is no need for related objects, only id are interesting. Less fixtures to write and maintain, less fixtures to load, faster tests. So in my opinion this proposal is a backward-incompatible feature request. Am I missing something?

comment:3 Changed 5 years ago by lukeplant

  • Type set to Bug

comment:4 Changed 5 years ago by lukeplant

  • Severity set to Normal

comment:5 Changed 5 years ago by ramiro

  • Easy pickings unset
  • Resolution set to duplicate
  • Status changed from new to closed
  • UI/UX unset

I'm going to close this as a duplicate of #11665.

Note: See TracTickets for help on using tickets.
Back to Top