首页 > 解决方案 > 在多对多连接中,我收到此错误:SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行

问题描述

在多对多加入中,我收到此错误:

使用消息'SQLSTATE [23000] 照亮/数据库/查询异常:完整性约束违规:1452 无法添加或更新子行:外键约束 t 失败(dbtest. note_tag, CONSTRAINT note_tag_note_id_foreignFOREIGN KEY ( note_id) REFERENCES tags( 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();
    }

标签: laravel

解决方案


您正在尝试在外键中将标签作为注释传递。

$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');

推荐阅读