douzhang2680 2017-09-03 21:23
浏览 84
已采纳

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败

I have 2 tables (note_tag and tags). When I want to create a tag, I get the error message:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (prj_test.note_tag, CONSTRAINT note_tag_note_id_foreign FOREIGN KEY (note_id) REFERENCES tags (id)) (SQL: insert into note_tag (note_id, tag_id) values (3, 1)).

Table

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');
        $table->integer('tag_id')->unsigned()->index();
        $table->foreign('tag_id')->references('id')->on('notes');
    });

NotesConroller

class NotesController extends Controller

    {
        public function store(Request $request, Card $card){

            $this->validate($request, [
               'body' => 'required|unique:notes|min:2'
            ]);

            $note = new Note($request->all());
            $note->user_id = 1;
            $card->notes()->save($note);

            $note->tags()->attach($request->input("tags"));

            flash("Note is saved security.", "succes");
            return back();
        }

        public function edit(Note $note){
            return view('notes.edit', compact('note'));
        }

        public function update(Note $note, Request $request){
            $note->update($request->all());
            return back();
        }
    }

show.blade.php

<div class="form-group">
    <select name="tags[]" title="tags" class="form-control" multiple="multiple">
        @foreach($tags as $tag)
                <option value="{{ $tag ->id }}">{{ $tag->name }}</option>
        @endforeach
    </select>
</div>

Tag.php

public function notes(){
    return $this->belongsToMany(Note::class);
}

I can't seem to find what I'm doing wrong. Obviously there is something wrong with the foreign key.

  • 写回答

2条回答 默认 最新

  • duanbin4847 2017-09-03 21:31
    关注

    You have incorrectly defined your foreign keys. note_id references the tags table, and tag_id the notes table in your code.

    It should instead be:

    Schema::create('note_tag', function (Blueprint $table) {
        $table->integer('note_id')->unsigned()->index();
        $table->foreign('note_id')->references('id')->on('notes');
        $table->integer('tag_id')->unsigned()->index();
        $table->foreign('tag_id')->references('id')->on('tags');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?