Ian Lewis 3/2/2010

'self' ForeignKeys always result in a JOIN

Read Original

This article details a specific Django ORM behavior where a ForeignKey referencing the same model ('self') always generates a SQL JOIN in filter queries, potentially impacting performance on large tables. The author demonstrates the issue with code examples and provides a solution using the QuerySet.extra() method to write a more efficient raw WHERE clause, avoiding the unwanted join.

'self' ForeignKeys always result in a JOIN

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week

1
The Beautiful Web
Jens Oliver Meiert 2 votes
3
LLM Use in the Python Source Code
Miguel Grinberg 1 votes
4
Wagon’s algorithm in Python
John D. Cook 1 votes