0
votes

I try to $this->MembersQuestionsAnswer->saveMany($data).

And $data is this:

array(
    (int) 0 => array(
        'member_id' => '4',
        'questions_anwser_id' => '102'
    ),
    (int) 1 => array(
        'member_id' => '4',
        'questions_anwser_id' => '203'
    )
)

then I got error

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (dating_sys.members_questions_answers, CONSTRAINT members_questions_answers_ibfk_2 FOREIGN KEY (questions_answer_id) REFERENCES questions_answers (id))

SQL Query: INSERT INTO dating_sys.members_questions_answers (member_id, modified) VALUES (4, '2012-11-14 01:13:27')

I checked the database. Both 102 and 203 do exist in table questions_answers id field. And I can also manually insert them into the table. Foreign key constraints work as I checked as well.

And the table members_questions_answers table DDL is

CREATE TABLE `members_questions_answers` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `member_id` int(10) unsigned NOT NULL,
  `questions_answer_id` int(10) unsigned NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `member_fk` (`member_id`) USING BTREE,
  KEY `questions_answer_fk` (`questions_answer_id`) USING BTREE,
  CONSTRAINT `members_questions_answers_ibfk_2` FOREIGN KEY (`questions_answer_id`) REFERENCES `questions_answers` (`id`),
  CONSTRAINT `members_questions_answers_ibfk_1` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

It feels like questions_answers table not really identified for some reason. I did have changed the name of this table and its foreign keys references. Is there a cache or something?

Can anyone help or point me to a new way to look into?

1
Is the model set to use the Tree Behavior?Dave
I found the problem. I misspelled 'answer' in the $data array to 'anwser'.Hao

1 Answers

0
votes

Yes it looks like a caching issue. Clear your model cache files in app/tmp/cache/models (or whatever cache engine you are using)