【发布时间】:2014-11-28 17:34:56
【问题描述】:
我在尝试使用 n_n 关系在 crud 中保存数据(或更新)时遇到问题,我有一个与 product_detail 相关的产品表,产品详细信息有 3 个外键,以连接到一个名为 color 的表,以及其他所谓的材料。知道为什么会失败吗?感谢您的帮助,谢谢。
错误是这样的:
发生数据库错误
错误号:1452
无法添加或更新子行:外键约束失败(medina_db.product_detail, CONSTRAINT product_detail_ibfk_3 FOREIGN KEY (material_id) REFERENCES material (id))
插入product_detail(product_id,color_id)值('4','1')
文件名:/Applications/MAMP/htdocs/industrias_medina/models/grocery_crud_model.php
行号:413
我的杂货店代码:
$crud = new grocery_CRUD();
$crud->set_table('product')->set_subject('Productos');
$crud->set_relation_n_n("Colores", 'product_detail', 'color', 'product_id', 'color_id', 'name');
$crud->set_relation_n_n("Materiales", 'product_detail', 'material', 'product_id', 'material_id', 'name');
$crud->set_field_upload('image','assets/uploads/files');
$crud->fields('name','model','description', "Colores", "Materiales", 'image');
$output = $crud->render();
$this->_productos_output($output);
SQL:
CREATE TABLE `color` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=ucs2 AUTO_INCREMENT=2 ;
CREATE TABLE `material` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Unique identifier',
`name` varchar(250) NOT NULL COMMENT 'Product''s name',
`model` varchar(250) NOT NULL COMMENT 'Product''s model',
`description` varchar(400) NOT NULL COMMENT 'Product''s description',
`image` varchar(400) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Table to store products' AUTO_INCREMENT=13 ;
CREATE TABLE `product_detail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`color_id` int(11) NOT NULL,
`material_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`product_id`,`color_id`,`material_id`),
KEY `product_id` (`product_id`),
KEY `color_id` (`color_id`),
KEY `material_id` (`material_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;
【问题讨论】:
标签: php codeigniter crud grocery-crud