#33608: select_related() to nullable FK with db_constraint=False do not promote OUTER joins. -------------------------------------+------------------------------------- Reporter: Mathieu Hinderyckx | Owner: nobody Type: Bug | Status: closed Component: Database layer | Version: 3.2 (models, ORM) | Severity: Normal | Resolution: invalid Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Mathieu Hinderyckx): Replying to [comment:3 Simon Charette]: > The question here is should the ORM account for possible integrity violations in the first place, lets not forget that `db_constraint` was basically introduced as [https://github.com/django/django/commit/b55cde054ee7dd22f93c3522a8ddb1d04193bcac some form of shadow option] without in-dept discussion about its desired behaviour with regards to violations. > > How the ORM should account for possible integrity violation was already discussed in the past plenty when usage of the MySQL's MyISAM backend (and SQLite in older versions) which doesn't support foreign keys was more common and caused users to run into this exact issue. > > Given we've opted not to special case JOIN'ing logic for `supports_foreign_keys` in the past I don't see why we should take a different stance for `db_constraint=False`. > > If we allowed `Object.objects.filter(remote_object_id=1234).select_related('remote_object')` to return `Object` instances what would their `.remote_object` be? A mock `RemoteObject(id=1234)` object? Simply `None`? Aren't both approaches breaking symmetry with `Object.objects.filter(remote_object_id=1234).first().remote_object` raising a `RemoteObject.DoesNotExist` exception? `Object.objects.filter(remote_object_id=1234).first().remote_object` doesn't raise a DoesNotExist exception. It returns null, as it should be. First it fetches the Object with `remote_object_id=1234` which exists. Secondly, It then accesses the FK field on that Object pointing to a RemoteObject, which is null as it does not exist in the remote table (hence why the db_constraint=False). That works as expected, just like an 'ordinary' FK. What doesn't work as expected is that adding a select_related() breaks the filter statement. Select_related is an optimization, the expectation is that it does the same two steps as before but with a single DB query. That isn't the case currently; it silently breaks the filtering logic, and doesn't event return the Object at all. -- Ticket URL: <https://code.djangoproject.com/ticket/33608#comment:5> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107017fe3eb506b-9c78b45b-2a2f-4256-aced-1bb52d785980-000000%40eu-central-1.amazonses.com.