4 ms·
Queries across joins are the simple example. The ORM takes care of the join bit for you so you can write: Book.objects.filter(author__address__country='GB'
by MatthewWilkes 10y ago
Queries across joins are the simple example. The ORM takes care of the join bit for you so you can write:
Book.objects.filter(author__address__country='GB')
Rather than having to manage the joins with the author and address tables yourself.
- ZenoArrow 10y agoWith that example, do you have control over how the records are joined? For example, in SQL if I wanted to return records where an records could be found in both tables, I would use an inner join, whereas if I wanted to return all records from 'author' and any related information from 'address' (and return NULL if a suitable address entry couldn't be found) I could use a left join. Does the ORM you have in mind give you that flexibility? It's probably worth mentioning that some SQL tools will suggest fields that can be joined on, if this is a concern.
- jsmeaton 10y agoDjango will generate a LEFT JOIN for nullable keys, and an INNER JOIN for non nullable keys. The lack of control over joins and conditions is a weakness of the ORM, but Django generally does the right thing. Of course, you can do an `__isnull` on the relation to create a pseudo inner join from the generated LEFT JOIN, but admit it's not the same.