php - Inserting Record into Database with CakePHP Results in Foreign Key Violation -


i'm cakephp (a few days) , i'm having issues inserting records foreign keys.

basic idea, warehouses.

i have warehouses table holds available warehouses , have warehouse_types table hold different types of warehouses. warehouse_types table has data in it.

i have simple add view (based off blog example in cakephp's cookbook) , have displaying names of different types database.

when save page database error.

error: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (`kvs`.`warehouses`, constraint `warehouses_ibfk_1` foreign key (`warehouse_type_id`) references `warehouse_types` (`id`)) 

the sql being generated is:

sql query: insert `kvs`.`warehouses` (`name`, `location`, `modified`, `created`) values ('name text', 'location text', '2014-04-02 12:07:13', '2014-04-02 12:07:13')  

if output information in request->data object, shows getting value warehousetype_id.

array(     'warehouse' => array(         'name' => 'name text',         'location' => 'location text',         'warehousetype_id' => '1'     ) ) 

any on awesome!

my database tables follows:

create table `warehouses` (   `id` int(10) unsigned not null auto_increment,   `name` varchar(50) default null,   `location` varchar(255) default null,   `warehouse_type_id` int(10) unsigned not null,   `created` datetime default null,   `modified` datetime default null,   primary key (`id`),   key `warehouse_type_id` (`warehouse_type_id`),   constraint `warehouses_ibfk_1` foreign key (`warehouse_type_id`) references `warehouse_types` (`id`) ) engine=innodb auto_increment=10 default charset=latin1;  create table `warehouse_types` (   `id` int(10) unsigned not null auto_increment,   `name` varchar(50) default null,   primary key (`id`) ) engine=innodb auto_increment=3 default charset=latin1; 

my models follows:

class warehouse extends appmodel{     public $belongsto = 'warehousetype'; }  class warehousetype extends appmodel{  } 

my view (add.ctp) follows:

<h1>add warehouse</h1> <?php echo $this->form->create('warehouse'); echo $this->form->input('name'); echo $this->form->input('location'); echo $this->form->input('warehousetype_id'); echo $this->form->end('create warehouse'); ?> 

finally, add method in warehousescontroller:

public function add(){         //check request see if post         if($this->request->is('post')){             $this->warehouse->create(); //make new warehouse object             debug($this->request->data);             if($this->warehouse->save($this->request->data)){ //try , save warehouse                 $this->session->setflash(__('warehouse '.$this->request->data['warehouse']['name'].' created'));                 return $this->redirect(array('action' => 'index')); //redirect warehouse list             }             $this->session->setflash(__('unable add warehouse :('));         }         $this->set('warehousetypes', $this->warehouse->warehousetype->find('list'));     } 

i've read on answer here seems relevant, i'm lost: cakephp can't insert record foreign key error

also reviewed documentation on saving: http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto

your column name different being used in cakephp. change cakephp use warehouse_type_id. (your cakephp code using warehousetype_id)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -