MySQL 违反完整性约束:1452 无法添加或更新子行:外键约束失败(Laravel 应用程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42470554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (Laravel Application)
提问by Angus Simons
I'm having this error in my laravel application.
我的 Laravel 应用程序中出现此错误。
these are the tables:
这些是表格:
Post
邮政
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
$table->integer('user_id')->unsigned();
});
Categories
类别
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Post_Category
Post_Category
Schema::create('post_category', function (Blueprint $table) {
$table->integer('post_id')->unsigned()->unique()->nullable();
$table->integer('cat_id')->unsigned()->unique()->nullable();
$table->timestamps();
});
Foreign Keys
外键
Schema::table('posts', function (Blueprint $table) {
$table->foreign('id')->references('post_id')->on('post_category')->onDelete('cascade');
});
Schema::table('categories', function (Blueprint $table) {
$table->foreign('id')->references('cat_id')->on('post_category')->onDelete('cascade');
});
Here my models
这是我的模型
class Categorie extends Model
{
protected $table = 'categories';
public function posts() {
return $this->hasMany('App\PostCategory');
}
}
...
...
class Post extends Model
{
public function author() {
return $this->belongsTo('App\User', 'user_id');
}
public function featuredImage() {
return $this->belongsTo('App\Media', 'media_id');
}
public function categories() {
return $this->hasMany('App\PostCategory');
}
}
...
...
class PostCategory extends Model
{
protected $table = 'post_category';
}
Here the controllers
这里的控制器
Store Category:
店铺类别:
public function store(StoreCategory $request)
{
$category = new Categorie;
$category->name = $request->input('name');
$category->save();
return redirect('/admin/categories')->with('status', 'New category created!');
}
Store post
店铺帖子
public function store(StorePost $request)
{
$post = new Post;
$post->title = $request->input('title');
$post->content = $request->input('content');
$post->media_id = $request->input('media_id');
$post->user_id = $request->input('user_id');
$post->save();
return redirect('/admin/posts')->with('status', 'New post created!');
}
Having this error
出现这个错误
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
the_film_corner
.tfc_categories
, CONSTRAINTcategories_id_foreign
FOREIGN KEY (id
) REFERENCEStfc_post_category
(cat_id
) ON DELETE CASCADE) (SQL: insert intotfc_categories
(name
,updated_at
,created_at
) values (News, 2017-02-26 14:51:15, 2017-02-26 14:51:15))
SQLSTATE[23000]: 完整性约束违规:1452 无法添加或更新子行:外键约束失败 (
the_film_corner
.tfc_categories
, CONSTRAINTcategories_id_foreign
FOREIGN KEY (id
) REFERENCEStfc_post_category
(cat_id
) ON DELETE CASCADE) (SQL: insert intotfc_categories
(name
,updated_at
,created_at
) values (News , 2017-02-26 14:51:15, 2017-02-26 14:51:15))
Thanks
谢谢
采纳答案by Samuele Colombo
You add the foreign keys in the wrong table:
您在错误的表中添加外键:
Schema::table('post_category', function(Blueprint $table) {
$table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('category')->onDelete('cascade');
});
Then...
然后...
class Categorie extends Model
{
protected $table = 'categories';
public function posts() {
return $this->belongsToMany('App\Category');
// Or more specifically
return $this->belongsToMany('App\Category', 'post_category', 'id', 'post_id');
}
}
class Post extends Model
{
// ...
public function categories() {
return $this->belongsToMany('App\Category');
// Or more specifically
return $this->belongsToMany('App\Category', 'post_category', 'id', 'category_id');
}
}
You can find more information following the official guide for Laravel:
您可以在 Laravel 的官方指南中找到更多信息:
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
回答by Monkey Supersonic
The error says everything. With this setup, before you insert a row into tfc_categories
, the value for the column id
has to exist in the column cat_id
of the table tfc_post_category
. If not, you violate the constraint mentioned in the failure message.
错误说明了一切。使用此设置,在向 中插入行之前tfc_categories
,列的值id
必须存在于cat_id
表的列中tfc_post_category
。如果不是,则您违反了失败消息中提到的约束。
But I rather guess you wanted one column to reference the other vice versa.
但我更愿意猜测您希望一列引用另一列,反之亦然。