Laravel 5.1 关系 - SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败
Laravel 5.1 Relationship - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
我有 3 个表用户,posts 和评论。 users 和 posts 之间的关系是可以的。我可以存储新的 posts。但是如果我评论 post,我会得到这个错误信息:
错误信息
QueryException in Connection.php line 631:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog
.comments
, CONSTRAINT comments_post_id_foreign
FOREIGN KEY (post_id
) REFERENCES posts
(id
)) (SQL: insert into comments
(body
, user_id
, updated_at
, created_at
) values (new comment, 1, 2015-07-20 14:16:07, 2015-07-20 14:16:07))
迁移用户
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
迁移posts
Schema::create( 'posts', function ( Blueprint $table ) {
$table->increments( 'id' );
$table->integer( 'user_id' )->unsigned();
$table->string( 'title' );
$table->text( 'body' );
$table->timestamps();
$table->foreign( 'user_id' )->references( 'id' )->on( 'users' );
} );
迁移评论
Schema::create('comments', function (Blueprint $table) {
$table->increments( 'id' );
$table->integer( 'user_id' )->unsigned();
$table->integer( 'post_id' )->unsigned();
$table->text( 'body' );
$table->timestamps();
$table->foreign( 'user_id' )->references( 'id' )->on( 'users' );
$table->foreign( 'post_id' )->references( 'id' )->on( 'posts' );
});
模型用户
protected $fillable = [
'name',
'email',
'password'
];
public function posts() {
return $this->hasMany( 'App\Post' );
}
public function comments() {
return $this->hasMany( 'App\Comment' );
}
型号Post
protected $fillable = [
'title',
'body'
];
public function user() {
return $this->belongsTo( 'App\User' );
}
public function comments() {
return $this->hasMany( 'App\Comment' );
}
模特评论
protected $fillable = [
'body'
];
public function user() {
return $this->belongsTo( 'App\User' );
}
public function post() {
return $this->belongsTo( 'App\Post' );
}
评论控制器
public function store( CommentRequest $request ) {
Auth::user()->comments()->create( $request->all() );
return redirect()->back();
}
Post控制器$request->all()
"_token" => "TzdItNKU3TChVg50vAqdy1CYCrIR562l4Wv2z6ef"
"title" => "New Post"
"body" => "New text"
CommentController $request->all()
"_token" => "TzdItNKU3TChVg50vAqdy1CYCrIR562l4Wv2z6ef"
"body" => "New Comment"
您的评论模型通过post_id[与Post模型有关系 字段,并且该字段在数据库中为 NOT NULL。因此 table 中的所有记录都需要填写该字段。此外,由于您对该字段有外键约束,它总是需要指向 Post table.
中的现有行
在你的情况下,似乎无论数据传递给你的 Auth::user()->comments()->create(),它都不包含post_id 字段或 post_id 字段未标记为 fillable 评论 型号。
我建议您首先检查控制器方法中的 $request->all() returns。如果该字段存在,请将 post_id 添加到 Comment[=30= 中的 fillable 字段列表中] 型号:
protected $fillable = [ /** list all fields that should be fillable by create/update methods **/ ];
我在互联网上发现这个技巧在这种情况下非常有效。
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::table('foo_products', function(Blueprint $table)
{
$table->unsignedInteger('foo_type_id');
$table->foreign('foo_type_id')->references('id')->on('foo_types');
});
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
您只需在模式之前禁用外键检查,然后再启用它。
我有 3 个表用户,posts 和评论。 users 和 posts 之间的关系是可以的。我可以存储新的 posts。但是如果我评论 post,我会得到这个错误信息:
错误信息
QueryException in Connection.php line 631: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
blog
.comments
, CONSTRAINTcomments_post_id_foreign
FOREIGN KEY (post_id
) REFERENCESposts
(id
)) (SQL: insert intocomments
(body
,user_id
,updated_at
,created_at
) values (new comment, 1, 2015-07-20 14:16:07, 2015-07-20 14:16:07))
迁移用户
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
迁移posts
Schema::create( 'posts', function ( Blueprint $table ) {
$table->increments( 'id' );
$table->integer( 'user_id' )->unsigned();
$table->string( 'title' );
$table->text( 'body' );
$table->timestamps();
$table->foreign( 'user_id' )->references( 'id' )->on( 'users' );
} );
迁移评论
Schema::create('comments', function (Blueprint $table) {
$table->increments( 'id' );
$table->integer( 'user_id' )->unsigned();
$table->integer( 'post_id' )->unsigned();
$table->text( 'body' );
$table->timestamps();
$table->foreign( 'user_id' )->references( 'id' )->on( 'users' );
$table->foreign( 'post_id' )->references( 'id' )->on( 'posts' );
});
模型用户
protected $fillable = [
'name',
'email',
'password'
];
public function posts() {
return $this->hasMany( 'App\Post' );
}
public function comments() {
return $this->hasMany( 'App\Comment' );
}
型号Post
protected $fillable = [
'title',
'body'
];
public function user() {
return $this->belongsTo( 'App\User' );
}
public function comments() {
return $this->hasMany( 'App\Comment' );
}
模特评论
protected $fillable = [
'body'
];
public function user() {
return $this->belongsTo( 'App\User' );
}
public function post() {
return $this->belongsTo( 'App\Post' );
}
评论控制器
public function store( CommentRequest $request ) {
Auth::user()->comments()->create( $request->all() );
return redirect()->back();
}
Post控制器$request->all()
"_token" => "TzdItNKU3TChVg50vAqdy1CYCrIR562l4Wv2z6ef"
"title" => "New Post"
"body" => "New text"
CommentController $request->all()
"_token" => "TzdItNKU3TChVg50vAqdy1CYCrIR562l4Wv2z6ef"
"body" => "New Comment"
您的评论模型通过post_id[与Post模型有关系 字段,并且该字段在数据库中为 NOT NULL。因此 table 中的所有记录都需要填写该字段。此外,由于您对该字段有外键约束,它总是需要指向 Post table.
中的现有行在你的情况下,似乎无论数据传递给你的 Auth::user()->comments()->create(),它都不包含post_id 字段或 post_id 字段未标记为 fillable 评论 型号。
我建议您首先检查控制器方法中的 $request->all() returns。如果该字段存在,请将 post_id 添加到 Comment[=30= 中的 fillable 字段列表中] 型号:
protected $fillable = [ /** list all fields that should be fillable by create/update methods **/ ];
我在互联网上发现这个技巧在这种情况下非常有效。
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::table('foo_products', function(Blueprint $table)
{
$table->unsignedInteger('foo_type_id');
$table->foreign('foo_type_id')->references('id')->on('foo_types');
});
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
您只需在模式之前禁用外键检查,然后再启用它。