完整性约束违规:1452无法添加或更新子行:
我试图插入值到我的评论表,我得到一个错误。 它说我不能添加或更新子行,我不知道这意味着什么。
我的模式看起来像这样
-- ---------------------------- -- Table structure for `comments` -- ---------------------------- DROP TABLE IF EXISTS `comments`; CREATE TABLE `comments` ( `id` varchar(36) NOT NULL, `project_id` varchar(36) NOT NULL, `user_id` varchar(36) NOT NULL, `task_id` varchar(36) NOT NULL, `data_type_id` varchar(36) NOT NULL, `data_path` varchar(255) DEFAULT NULL, `message` longtext, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_comments_users` (`user_id`), KEY `fk_comments_projects1` (`project_id`), KEY `fk_comments_data_types1` (`data_type_id`), CONSTRAINT `fk_comments_data_types1` FOREIGN KEY (`data_type_id`) REFERENCES `data_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_comments_projects1` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_comments_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf32; -- ---------------------------- -- Records of comments -- ---------------------------- -- ---------------------------- -- Table structure for `projects` -- ---------------------------- DROP TABLE IF EXISTS `projects`; CREATE TABLE `projects` ( `id` varchar(36) NOT NULL, `user_id` varchar(36) NOT NULL, `title` varchar(45) DEFAULT NULL, `description` longtext, `created` datetime DEFAULT NULL, `modified` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_projects_users1` (`user_id`), CONSTRAINT `fk_projects_users1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf32; -- ---------------------------- -- Records of projects -- ---------------------------- INSERT INTO `projects` VALUES ('50dcbc72-3410-4596-8b71-0e80ae7aaee3', '50dcbc5c-d684-40bf-9715-0becae7aaee3', 'Brand New Project', 'This is a brand new project', '2012-12-27 15:24:02', '2012-12-27 15:24:02');
和我想要做的mysql语句看起来像这样
INSERT INTO `anthonyl_fbpj`.`comments` (`project_id`, `user_id`, `task_id`, `data_type_id`, `message`, `modified`, `created`, `id`) VALUES ('50dc845a-83e4-4db3-8705-5432ae7aaee3', '50dcbc5c-d684-40bf-9715-0becae7aaee3', '1', '50d32e5c-abdc-491a-a0ef-25d84e9f49a8', 'this is a test', '2012-12-27 19:20:46', '2012-12-27 19:20:46', '50dcf3ee-8bf4-4685-aa45-4eb4ae7aaee3')
我得到的错误看起来像这样
SQLSTATE [23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
anthonyl_fbpj
,CONSTRAINTfk_comments_projects1
FOREIGN KEY(project_id
)REFERENCESprojects
(id
)ON DELETE NO动作ON UPDATE NO动作)
这仅仅意味着您要插入的表comments
上的列project_id
的值在表projects
中不存在。 请记住,表comments
的列project_id
的值取决于表Projects
上的ID
值。
表projects
中不存在您为列project_id
插入的值50dc845a-83e4-4db3-8705-5432ae7aaee3
。
确保你的Comment
模型的fillable
属性中有project_id
。
我有同样的问题,这就是原因。
还要确保你添加的外键是原始列的相同types,如果你引用的列不是相同的types,它也会失败。
如果有人正在使用Laravel并正在解决这个问题。 我得到这个以及问题是在我插入数据透视表中的ID(即外键)的顺序。
具体来说,find一个多对多关系的例子:
wordtokens < – > wordtoken_wordchunk < – > wordchunks
// wordtoken_wordchunk table Schema::create('wordtoken_wordchunk', function(Blueprint $table) { $table->integer('wordtoken_id')->unsigned(); $table->integer('wordchunk_id')->unsigned(); $table->foreign('wordtoken_id')->references('id')->on('word_tokens')->onDelete('cascade'); $table->foreign('wordchunk_id')->references('id')->on('wordchunks')->onDelete('cascade'); $table->primary(['wordtoken_id', 'wordchunk_id']); }); // wordchunks table Schema::create('wordchunks', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); $table->string('text'); }); // wordtokens table Schema::create('word_tokens', function (Blueprint $table) { $table->increments('id'); $table->string('text'); });
现在我的模型如下所示:
class WordToken extends Model { public function wordchunks() { return $this->belongsToMany('App\Wordchunk'); } } class Wordchunk extends Model { public function wordTokens() { return $this->belongsToMany('App\WordToken', 'wordtoken_wordchunk', 'wordchunk_id', 'wordtoken_id'); } }
我通过在Wordchunk模型中交换“wordchunk_id”和“wordtoken_id”的顺序来解决这个问题。
对于代码完成,这是我坚持模型:
private function persistChunks($chunks) { foreach ($chunks as $chunk) { $model = new Wordchunk(); $model->text = implode(' ', array_map(function($token) {return $token->text;}, $chunk)); $tokenIds = array_map(function($token) {return $token->id;}, $chunk); $model->save(); $model->wordTokens()->attach($tokenIds); } }