Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Finder :include assoc with same name as key fail on Postgres

#274

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.

Reported by Martin Sadler · May 29th, 2008 @ 05:31 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung

    It seems to me that you are already skating on thin ice by doing this.

    May 29th, 2008 @ 10:59 PM

  2. Martin Sadler
    Martin Sadler

    Frederick, thanks for the comment. Would you care to expand on that?

    May 30th, 2008 @ 08:26 AM

  3. Frederick Cheung
    Frederick Cheung

    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)

    May 30th, 2008 @ 10:00 AM

  4. Pratik
    Pratik
    • State changed from new to incomplete
    • Tag set to activerecord, associations, bug, edge, patch, postgres

    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.

    July 2nd, 2008 @ 02:18 AM

  5. Martin Sadler
    Martin Sadler

    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.

    July 2nd, 2008 @ 08:46 AM

  6. Pratik
    Pratik
    • State changed from incomplete to wontfix

    July 2nd, 2008 @ 12:51 PM