PGError: ERROR: column "id" does not exist Hide options

11 views
Skip to first unread message

Bill Burcham

unread,
Dec 3, 2009, 7:25:17 PM12/3/09
to Rails Authorization Plugin
Over on thread http://groups.google.com/group/rails-authorization-plugin/browse_thread/thread/db2f0f8995bef55a
I notice that somebody had trouble creating objects because the
roles_users table has no 'id' column. I wasn't having the problem on
create, but I was having it when destroying an acts_as_authorizable
model.

Mysql::Error: Unknown column 'id' in 'where clause': DELETE FROM
`roles_users` WHERE (`id` IN (NULL))

Over on the other thread, somebody suggested the
composite_primary_keys gem. I tried that and it did fix the immediate
problem, however, it caused other problems in my app. Specifically, a
belongs_to with a non-standard foreign-key column name like this:

belongs_to :inviter, :class_name => 'User'

Won't work once you drop in the composite_primary_keys gem. The
offending code in that gem is:

[3, 12] in /Library/Ruby/Gems/1.8/gems/composite_primary_keys-2.3.2/
lib/composite_primary_keys/reflection.rb
3 class AssociationReflection
4 def primary_key_name
5 return @primary_key_name if @primary_key_name
6 case
7 when macro == :belongs_to
=> 8 @primary_key_name = options[:foreign_key] ||
class_name.foreign_key
9 when options[:as]

Which generates 'user_id' (incorrect).

Compare to the way Rails 2.3.4 generates the primary key name:

# File vendor/rails/activerecord/lib/active_record/reflection.rb,
line 212
212: def derive_primary_key_name
213: if macro == :belongs_to
214: "#{name}_id"

Which generates 'inviter_id' (correct).
Reply all
Reply to author
Forward
0 new messages