laravel - 在多对多连接中,我收到此错误:SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行
问题描述
在多对多加入中,我收到此错误:
使用消息'SQLSTATE [23000] 照亮/数据库/查询异常:完整性约束违规:1452 无法添加或更新子行:外键约束 t 失败(
dbtest
.note_tag
, CONSTRAINTnote_tag_note_id_foreign
FOREIGN KEY (note_id
) REFERENCEStags
(id
) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: 插入note_tag
(note_id
,tag_id
) 值 (28, 1))'
笔记控制器:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
protected $fillable = [
'body',
];
public function card()
{
return $this->belongsTo(Card::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}
}
标签控制器:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
public function notes()
{
return $this->belongsToMany(Note::class);
}
}
迁移=> 标签:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('note_tag', function (Blueprint $table) {
$table->integer('note_id')->unsigned()->index();
$table->foreign('note_id')->references('id')->on('tags')
->onDelete('cascade')->onUpdate('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('notes')
->onDelete('cascade')->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
Schema::dropIfExists('note_tag');
}
}
鉴于:
<form action="/cards/{{$card->id}}/note" method="post">
{{csrf_field()}}
<textarea name="body" title="body" placeholder="Please Enter Your Note....">{{old('body')}}</textarea>
<select style="display: block; width: 300px;margin-top: 20px" name="tags[]" title="tags" multiple>
@foreach($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
@endforeach
</select>
<button type="submit">Add Note</button>
</form>
在控制器中:
public function store(Card $card,Request $request)
{
//dd($request->input('tags'));
$this->validate($request,[
'body' => 'required|min:6'
]);
$note = new Note($request->all());
$note->user_id = 1;
$card->notes()->save($note);
$tagsId = $request->input('tags');
$note->tags()->attach($tagsId);
//\Session::flash('flash_message','Your note has been created.');
//$request->session()->flash('flash_message','Your note has been created.');
flash('note created successfully');
return back();
}
解决方案
您正在尝试在外键中将标签作为注释传递。
$table->foreign('tag_id')->references('id')->on('notes')
->onDelete('cascade')->onUpdate('cascade');
应该->on(tags)
$table->foreign('tag_id')->references('id')->on('tags')
->onDelete('cascade')->onUpdate('cascade');
推荐阅读
- spring-boot - 没有域的后端服务器的 SSL
- ios - 为什么我的 IOS 应用程序模拟器停止显示我的应用程序屏幕
- python - pyfcm.errors.AuthenticationError:验证发件人帐户时出错
- mongodb - limit() 选项在 mongodb 中如何工作?
- spring-boot - HBM2DDL 正在尝试跨目标数据库以外的数据库映射实体
- ruby - 错误:安装 XSpear-1.4.1.gem 时出错:rubyzip 需要 Ruby 版本 >= 2.4
- c++ - 如何在 DPC++ 编译器选项中指定 C++ 标准版本?
- java - Apache Flink:如何拆分组合广播状态并在 processBroadcastElement() 中放入单独的 MapStateDescriptor
- angular - 如何在 Angular 7 中将渲染添加到外部 URL?
- javascript - 作为道具传递的函数不是函数