QueryException (23000) SQLSTATE[23000]:完整性约束 v1452
Posted
技术标签:
【中文标题】QueryException (23000) SQLSTATE[23000]:完整性约束 v1452【英文标题】:QueryException (23000) SQLSTATE[23000]: Integrity constraint v1452 【发布时间】:2017-11-13 06:05:53 【问题描述】:我被这个错误吸引了,因为这周有人可以帮我吗!
SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(practice2
.posts
, CONSTRAINT posts_category_id_foreign
FOREIGN KEY (category_id
) REFERENCES categories
(@987654326 @)) (SQL: 插入posts
(author_id
, updated_at
, created_at
) 值 (2, 2017-11-13 05:48:53, 2017-11-13 05:48:53) )
我的代码是: 博客控制器:
public function store(Requests\PostRequest $request)
$request->user()->posts()->create($request->all());
redirect('/backend/blog')->with('message', 'Your post was created successfully!');
**migration:**
public function up()
Schema::create('posts', function (Blueprint $table)
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('tittle');
$table->string('slug')->unique;
$table->text('excerpt');
$table->text('body');
$table->string('image')-> nullable();
$table->timestamps();
);
alter_post_migration:
public function up()
Schema::table('posts', function (Blueprint $table)
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
//
);
创建帖子表单:
<div class="box-body ">
!! Form::model($post, [
'method' => 'POST',
'route' => 'back.store'
]) !!
<div class="form-group $errors->has('tittle') ? 'has-error' : '' ">
!! Form::label('tittle') !!
!! Form::text('tittle', null, ['class' => 'form-control']) !!
@if($errors->has('tittle'))
<span class="help-block"> $errors->first('tittle') </span>
@endif
</div>
<div class="form-group $errors->has('slug') ? 'has-error' : '' ">
!! Form::label('slug') !!
!! Form::text('slug', null, ['class' => 'form-control']) !!
@if($errors->has('slug'))
<span class="help-block"> $errors->first('slug') </span>
@endif
</div>
<div class="form-group">
!! Form::label('excerpt') !!
!! Form::textarea('excerpt', null, ['class' => 'form-control']) !!
</div>
<div class="form-group $errors->has('body') ? 'has-error' : '' ">
!! Form::label('body') !!
!! Form::textarea('body', null, ['class' => 'form-control']) !!
@if($errors->has('body'))
<span class="help-block"> $errors->first('body') </span>
@endif
</div>
<div class="form-group $errors->has('published_at') ? 'has-error' : '' ">
!! Form::label('published_at', 'Publish Date') !!
!! Form::text('published_at', null, ['class' => 'form-control', 'placeholder' => 'Y-m-d H:i:s']) !!
@if($errors->has('published_at'))
<span class="help-block"> $errors->first('published_at') </span>
@endif
</div>
<div class="form-group $errors->has('category_id') ? 'has-error' : '' ">
!! Form::label('category_id', 'Category') !!
!! Form::select('category_id', App\Category::pluck('tittle', 'id'), null, ['class' => 'form-control', 'placeholder' => 'Choose category']) !!
@if($errors->has('category_id'))
<span class="help-block"> $errors->first('category_id') </span>
@endif
</div>
<hr>
!! Form::submit('Create new post', ['class' => 'btn btn-primary']) !!
!! Form::close() !!
</div>
【问题讨论】:
错误很明显 - 您正在尝试插入没有有效category_id
的 posts
记录。很可能将category_id
默认为空,并且没有空category_id
行。建议添加默认类别(例如 0)并将未分类的帖子设置为此,或更改 posts.category_id
以允许空值。
我后来修改了表,posts表中有category_id字段
【参考方案1】:
错误:
违反完整性约束:1452 无法添加或更新子行: 外键约束失败(practice2.posts,CONSTRAINT posts_category_id_foreign FOREIGN KEY (category_id) 参考 类别 (id))
当两个表共享foreign key
的关系并且您试图在子表中添加一些数据并且其关联的记录在父表中不存在时出现。因此,请相应地检查数据并重试。
【讨论】:
以上是关于QueryException (23000) SQLSTATE[23000]:完整性约束 v1452的主要内容,如果未能解决你的问题,请参考以下文章
休眠:org.hibernate.QueryException
org.hibernate.QueryException-- 无法解析属性
Illuminate\Database\QueryException:SQLSTATE[42000]:语法错误或访问冲突:1064