简体   繁体   English

杂货杂货-CodeIgniter related_n_n错误

[英]Grocery Crud - CodeIgniter relation_n_n error

I have a problem while trying to save data (or update) in a crud with n_n relation, i have a product table, who is related to a product_detail, product detail has 3 foreign keys, to connect to a table called color, and other called material. 我在尝试通过n_n关系保存数据(或更新)时遇到问题,我有一个产品表,该表与product_detail相关,产品详细信息具有3个外键,以连接到名为color的表,以及其他称为材料。 Any idea why fail?, i appreciate your help, thanks. 知道为什么会失败吗?,我感谢您的帮助,谢谢。

The error is this: 错误是这样的:

A Database Error Occurred 发生数据库错误

Error Number: 1452 错误号:1452

Cannot add or update a child row: a foreign key constraint fails ( medina_db . product_detail , CONSTRAINT product_detail_ibfk_3 FOREIGN KEY ( material_id ) REFERENCES material ( id )) 不能添加或更新子行,外键约束失败( medina_dbproduct_detail ,约束product_detail_ibfk_3外键( material_id )参考materialid ))

INSERT INTO product_detail ( product_id , color_id ) VALUES ('4', '1') 插入到product_detailproduct_idcolor_id )值('4','1')

Filename: /Applications/MAMP/htdocs/industrias_medina/models/grocery_crud_model.php 文件名:/Applications/MAMP/htdocs/industrias_medina/models/grocery_crud_model.php

Line Number: 413 行号:413


My Grocery Crud code: 我的食品杂货代码:

$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: 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 ;

It looks like your product_detail table has a material_id column that references the material table. 您的product_detail表似乎具有一个引用该材料表的material_id列。 Your insert statement is not inserting anything into that material_id column. 您的insert语句未在该material_id列中插入任何内容。 You need to either insert a value into that column (that exists in material), set a default value that references the key in material, or set a default of NULL for that column. 您需要在该列(材料中存在)中插入一个值,设置引用材料中键的默认值,或者为该列设置默认值NULL。

Thanks, 谢谢,

Andrew 安德鲁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM