首页 文章

完整性约束违规:1452无法添加或更新子行:外键约束失败(Laravel应用程序)

提问于
浏览
1

我的laravel应用程序中出现此错误 .

这些是表格:

Post

Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('content');
        $table->timestamps();
        $table->integer('user_id')->unsigned();
});

Categories

Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
});

Post_Category

Schema::create('post_category', function (Blueprint $table) {
       $table->integer('post_id')->unsigned()->unique()->nullable();
       $table->integer('cat_id')->unsigned()->unique()->nullable();
       $table->timestamps();
});

Foreign Keys

Schema::table('posts', function (Blueprint $table) {
       $table->foreign('id')->references('post_id')->on('post_category')->onDelete('cascade');
});

Schema::table('categories', function (Blueprint $table) {
      $table->foreign('id')->references('cat_id')->on('post_category')->onDelete('cascade');
});

Here my models

class Categorie extends Model
{
    protected $table = 'categories';

    public function posts() {
      return $this->hasMany('App\PostCategory');
    }
}

...

class Post extends Model
{
    public function author() {
      return $this->belongsTo('App\User', 'user_id');
    }

    public function featuredImage() {
      return $this->belongsTo('App\Media', 'media_id');
    }

    public function categories() {
      return $this->hasMany('App\PostCategory');
    }
}

...

class PostCategory extends Model
{
  protected $table = 'post_category';
}

Here the controllers

商店类别:

public function store(StoreCategory $request)
    {
        $category = new Categorie;
        $category->name = $request->input('name');
        $category->save();

        return redirect('/admin/categories')->with('status', 'New category created!');
    }

存储帖子

public function store(StorePost $request)
    {
        $post = new Post;
        $post->title = $request->input('title');
        $post->content = $request->input('content');
        $post->media_id = $request->input('media_id');
        $post->user_id = $request->input('user_id');
        $post->save();

        return redirect('/admin/posts')->with('status', 'New post created!');
    }

有这个错误

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败(the_film_corner.tfc_categories,CONSTRAINT categories_id_foreign FOREIGN KEY(id)REFERENCES tfc_post_category(cat_id)ON DELETE CASCADE)(SQL:insert into tfc_categories(name,updated_at,created_at)的值(新闻,2017-02-26 14:51:15,2017-02-26 14:51:15))

谢谢

2 回答

  • 0

    错误说明了一切 . 使用此设置,在将行插入 tfc_categories 之前,列 id 的值必须存在于表 tfc_post_categorycat_id 列中 . 如果不是,则违反了失败消息中提到的约束 .

    但我猜你想要一列引用另一列反之亦然 .

  • 2

    您在错误的表中添加外键:

    Schema::table('post_category', function(Blueprint $table) {
        $table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
        $table->foreign('category_id')->references('id')->on('category')->onDelete('cascade');
    });
    

    然后...

    class Categorie extends Model
    {
        protected $table = 'categories';
    
        public function posts() {
          return $this->belongsToMany('App\Category');
    
         // Or more specifically
         return $this->belongsToMany('App\Category', 'post_category', 'id', 'post_id');
        }
    }
    
    class Post extends Model
    {
        // ...
    
        public function categories() {
          return $this->belongsToMany('App\Category');
    
          // Or more specifically
          return $this->belongsToMany('App\Category', 'post_category', 'id', 'category_id');
        }
    }
    

    您可以在Laravel官方指南中找到更多信息:

    https://laravel.com/docs/5.4/eloquent-relationships#one-to-many

相关问题