Unnecessary sql query when loading a belongs_to association #2828

Closed
pixeltrix opened this Issue Sep 3, 2011 · 4 comments

Comments

Projects
None yet
4 participants
Owner

pixeltrix commented Sep 3, 2011

When loading a belongs_to relationship the association code executes the query even if the foreign key is nil - I think we can safely assume that there isn't a record with a NULL primary key!

pixeltrix was assigned Sep 3, 2011

Owner

tenderlove commented Sep 3, 2011

The primary key name is null, or the value? It seems possible that someone could have a column that allows nulls, even for the primary key.

Owner

pixeltrix commented Sep 3, 2011

The value is null. I can't say about other databases but I'm sure that MySQL doesn't allow NULL values in a primary key column. I suppose the belongs_to could be pointed to a non-primary key column that allows NULL values but I can't think of any use case for such a scenario.

Contributor

thedarkone commented Sep 3, 2011

+1 @pixeltrix. I also find the useless WHERE pk_column IS NULL loads annoying.

Member

jonleighton commented Sep 3, 2011

Sounds good to me. Please feel free to patch :)

@georg georg pushed a commit to georg/rails that referenced this issue Sep 5, 2011

Georg Friedrich Don't find belongs_to target when the foreign_key is NULL. Fixes #2828 d08f2d3

tenderlove closed this in 78c896c Sep 5, 2011

@brunoadacosta brunoadacosta pushed a commit to brunoadacosta/rails that referenced this issue Sep 5, 2011

@tenderlove Georg Friedrich + tenderlove Don't find belongs_to target when the foreign_key is NULL. Fixes #2828 2175012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment