I am sucked by this error since this week could any one help me please !
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (practice2
.posts
, CONSTRAINT posts_category_id_foreign
FOREIGN KEY (category_id
) REFERENCES categories
(id
)) (SQL: insert into posts
(author_id
, updated_at
, created_at
) values (2, 2017-11-13 05:48:53, 2017-11-13 05:48:53))
my codes are: Blog controller :
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');
//
});
}
create post form:
<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>
答案 0 :(得分:0)
错误:
完整性约束违规:1452无法添加或更新子行: 外键约束失败(practice2.posts,CONSTRAINT posts_category_id_foreign FOREIGN KEY(category_id)参考 类别(id))
当两个表共享foreign key
的关系并且您尝试在子表中添加一些数据并且其相关记录在父表中不存在时出现。因此,请相应地检查数据,然后重试。