开发者

Delete from table?

开发者 https://www.devze.com 2023-02-25 07:39 出处:网络
I still have following error: I want to delete question, but I get this: ActiveRecord::StatementInvalid (Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fail开发者_运维技

I still have following error: I want to delete question, but I get this:

ActiveRecord::StatementInvalid (Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fail开发者_运维技巧s (`survey_development`.`inquiries`, CONSTRAINT `inquiries_ibfk_1` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`)): DELETE FROM `questions` WHERE `id` = 217):

my controller

  def destroy
#    @question.destroy
#    head :ok
#    @question = Question.find(params[:id]) 
    @question.destroy   
    head :ok
  end

model

class Question < ActiveRecord::Base
  has_one :answer, :through => :inquiry , :dependent => :destroy
  belongs_to :inquiry , :dependent => :destroy

  validates_presence_of :text, :message => "ERROR!"
end

I'm stuck with this :(


You can't have the :dependent => :destroy on the Question model because it is the child object of Inquiries. This is what is invoking the query that is causing your problem.


looks like belongs_to :inquiry , :dependent => :destroy should be belongs_to :inquiry and in Inquiry model it should be has_one :question, :dependent => :destroy

you can't really have :dependent => :destroy on the child object

0

精彩评论

暂无评论...
验证码 换一张
取 消