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

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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建