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