Ravi Sankar Raju
Ravi Sankar Raju

Reputation: 2958

dependent nullify fails in a particular scenario

These are the two models that i have.

One(:id, :two_nd_id) and Two(:id, :nd_id)

Associations

1) One belongs_to :two, :foreign_key => 'two_nd_id', :primary_key => 'nd_id'

2) Two has_many :one, :foreign_key => 'two_nd_id', :primary_key => 'nd_id', :dependent => :nullify

Now, when i try to delete an object of Two it raises an error,

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'one.nd_id' in 'where clause': 
UPDATE `one` SET `two_nd_id` = NULL WHERE `one`.`two_nd_id` = 'xxxxx' AND `one`.`nd_id` IS 
NULL

I dont get why 'one'.'nd_id' IS NULL is checked at all ! Help me out. Thanks in advance

Note - :dependent => :destroy worked fine !

Upvotes: 1

Views: 547

Answers (1)

lulalala
lulalala

Reputation: 17991

Please say what your primary key for each model really is first.

Other than that:

First, I don't think you need to have :primary_key => 'nd_id'

Second, you should have Two has_many :ones, note the "s"

Third, why are both foreign key specified as "two_nd_id"? One of them is wrong.

Upvotes: 0

Related Questions