gpt4 book ai didi

mysql - 查询异常(23000)SQLSTATE [23000] : Integrity constraint v1452

转载 作者:行者123 更新时间:2023-11-29 18:12:50 24 4
gpt4 key购买 nike

这个错误让我很困惑,因为这周有人可以帮助我吗!SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(practice2.posts,CONSTRAINT posts_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (id)) (SQL: insert into posts ( author_idupdated_atcreated_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>

最佳答案

错误:

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))

当两个表共享外键关系并且您尝试在子表中添加一些数据并且其关联记录在父表中不存在时,就会出现这种情况。因此请相应地检查数据并重试。

关于mysql - 查询异常(23000)SQLSTATE [23000] : Integrity constraint v1452,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47258013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com