Finder :include assoc with same name as key fail on Postgres
Reported by Martin Sadler | May 29th, 2008 @ 05:31 PM
On Postgres when calling find on a model with an :include that contains an association with the same name as it's key an exception is raised:
>> Project.find(:all, :include => 'created_by')
=> Message: <"PGError: ERROR: invalid input syntax for integer: \"#<Person:0x19e0038>\"\n: SELECT * FROM \"people\" WHERE (\"people\".id IN (E'#<Person:0x19e0038>')) ">
The basic setup is as follows:
create_table :projects, :force => true do |t|
t.string :name
t.string :type
t.integer :created_by
end
class Project < ActiveRecord::Base
belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by'
...
end
This largely effects people who have used created_by, updated_by in their models. See: http://wiki.rubyonrails.org/rail...
However there is likely to be other edge cases
I have attached the failing test case and working fix as a patch.
Notes:
1) An argument against this patch could be that best practice should be to use a column named created_by_id instead. However I would probably favor a deprecation notice / warning over this to begin with.
2) This currently only affects Postgres and not MySQL. Given both adapters use the same code fix it suggests there maybe a deeper issue present.
Comments and changes to this ticket
-

Frederick Cheung May 29th, 2008 @ 10:59 PM
It seems to me that you are already skating on thin ice by doing this.
-

Martin Sadler May 30th, 2008 @ 08:26 AM
Frederick, thanks for the comment. Would you care to expand on that?
-

Frederick Cheung May 30th, 2008 @ 10:00 AM
Just stuff that would normally work like update_attributes (eg if you were updating a record) and generally other stuff that assumes it can use the accessors to access an attribute (which code usually should because you could have overridden that accessor with good reason)
-
Pratik July 2nd, 2008 @ 02:18 AM
- → Tag changed from to activerecord associations bug edge patch postgres
- → State changed from new to incomplete
I'm getting 2 tests failures on MySQL after applying this.
I kinda agree with Fred that you're already walking on thin ice, if not skating.
I think it'd be much easier if you just call your association 'creator' or something similar, which is not a column name.
But if the fix is very simple ( as it is in the patch ), probably wouldn't hurt to apply.
-

Martin Sadler July 2nd, 2008 @ 08:46 AM
Hey Pratik, I would close this ticket for now.
I ended up renaming created_by/updated_by to created_by_id/updated_by_id in the database and made a few tweaks to the plugin to handle the change.
Makes more sense and goes with the Rails way of thinking.
-
Pratik July 2nd, 2008 @ 12:51 PM
- → State changed from incomplete to wontfix
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
