Unknown column 'id' in 'where clause': DELETE FROM `roles` WHERE `id` = NULL

Hi,

I have a Modul model and a Role model. Each modul can have many roles.
So when the modul is deleted, it should delete the roles also.
I have the following in my Modul model:

has_many :roles, :dependent => :destroy

When i run some_object.destroy from the console, i get the following
error:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column ‘id’ in
‘where clause’: DELETE FROM roles WHERE id = NULL
from C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/
activerecord-2.3.3/lib/active_record/connection_adapters/abstract_ada
pter.rb:212:in log' from C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/ activerecord-2.3.3/lib/active_record/connection_adapters/mysql_adapte r.rb:320:inexecute’
from C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/
activerecord-2.3.3/lib/active_record/connection_adapters/abstract/dat
abase_statements.rb:265:in update_sql' from C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/ activerecord-2.3.3/lib/active_record/connection_adapters/mysql_adapte r.rb:335:inupdate_sql’
from C:/rubyonrails/ruby/lib/ruby/gems/1.8/gems/
activerecord-2.3.3/lib/active_record/connection_adapters/abstract/dat

Does anyone have any idea?

Thanks,
Pratik

Role Model:

create_table :roles, :id => false do |t|
t.integer :user_id
t.integer :modul_id
t.string :role
end

Modul Model:

create_table :moduls do |t|
t.string :name
t.integer :updated_by
t.timestamps
end

On Sep 12, 9:32 pm, tispratik [email protected] wrote:

Role Model:

create_table :roles, :id => false do |t|
t.integer :user_id
t.integer :modul_id
t.string :role
end

There’s your problem - rails expects your model to have a primary key;
your one doesn’t

Fred

On Sep 13, 2:51 pm, tispratik [email protected] wrote:

Then what is the workaround to this?

Why not let roles have a primary key? - there will be other stuff that
breaks if it doesn’t

Fred

2009/9/13 tispratik [email protected]:

Then what is the workaround to this?

Just remove the id => false from the roles table.

Colin

Then what is the workaround to this?
I even tried this in Modul.rb
before_destroy { |record| Role.destroy_all “modul_id = #{record.id}” }

can i run a sql query of my own ?

Thanks,
Pratik