Code

Opened 4 years ago

Closed 3 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

Description

When referencing a non-existing entity instance in fixtures for ForeignKey relationship no error message is thrown in test runner mode (./manage.py 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 ./manage.py testserver data.json I actually get an exception thrown.

Backend is Postgresql 8.3.


Model:

  5 class Parent(models.Model):
  6     child = models.ForeignKey('Child')
  7
  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     }



Test:

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


Attachments (0)

Change History (5)

comment:1 Changed 4 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 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 3 years ago by lukeplant

  • Type set to Bug

comment:4 Changed 3 years ago by lukeplant

  • Severity set to Normal

comment:5 Changed 3 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.