CakePHP Can't insert record with foreign key error -
i'm getting following error when trying insert record table:
error: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (invoice.quotes, constraint quotes_ibfk_1 foreign key (contacts_id) references contacts (id))
i have relationship setup between 2 tables 'contacts' , 'quotes'. 'quotes' has foreign key set contacts_id.
the add method in quotes controller looks this:
public function add() { $this->log('quote controller --> add method...1'); $this->log($this->request->data); if ($this->request->is('post')) { $this->quote->create(); // line writes details database. if ($this->quote->save($this->request->data)) { $this->session->setflash('your quote has been saved.'); $this->redirect(array('action' => 'index')); } else { $this->session->setflash('unable add quote.'); } } } any appreciated.
the error happens when don't have correct foreign key when trying save data.
i guessing "contacts_id" value inside $this->request->data null / empty. might want add debug($this->request->data); after $this->quote->create();
and seems don't follow convention of cakephp. contacts_id field should contact_id. might fix problem.
Comments
Post a Comment