tispratik
2009-Sep-12 20:29 UTC
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:in `execute''
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:in `update_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
tispratik
2009-Sep-12 20:32 UTC
Re: Unknown column ''id'' in ''where clause'': DELETE FROM `roles` WHERE `id` = NULL
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
Frederick Cheung
2009-Sep-12 23:11 UTC
Re: Unknown column ''id'' in ''where clause'': DELETE FROM `roles` WHERE `id` = NULL
On Sep 12, 9:32 pm, tispratik <tispra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> Modul Model: > =========> create_table :moduls do |t| > t.string :name > t.integer :updated_by > t.timestamps > end
tispratik
2009-Sep-13 13:51 UTC
Re: Unknown column ''id'' in ''where clause'': DELETE FROM `roles` WHERE `id` = NULL
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
Frederick Cheung
2009-Sep-13 14:55 UTC
Re: Unknown column ''id'' in ''where clause'': DELETE FROM `roles` WHERE `id` = NULL
On Sep 13, 2:51 pm, tispratik <tispra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> 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
Colin Law
2009-Sep-13 17:02 UTC
Re: Unknown column ''id'' in ''where clause'': DELETE FROM `roles` WHERE `id` = NULL
2009/9/13 tispratik <tispratik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Then what is the workaround to this?Just remove the id => false from the roles table. Colin> 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 > > >