【问题标题】:Laravel 5.2 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint failsLaravel 5.2 - 完整性约束违规:1452 无法添加或更新子行:外键约束失败
【发布时间】:2016-10-27 09:48:26
【问题描述】:

这个问题有already been asked many times,我浏览了所有答案,但没有一个能解决我遇到的错误。

我正在使用 Laravel 5.2

我有 2 个表格 - 分类和类别。当我想创建分类时,我收到错误消息:

SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(myclassified.classifieds,CONSTRAINT classifieds_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (id))

迁移文件定义如下:

对于classifieds 表:

public function up()
{
    Schema::create('classifieds', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('description');
        $table->string('price');
        $table->timestamps();
    });
}

public function down()
{
    Schema::drop('classifieds');
}

对于categories 表:

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

public function down()
{
    Schema::drop('categories');
}

并添加外键,

 public function up()
{
    Schema::table('classifieds', function(Blueprint $table) {
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories');
    });
}

public function down()
{
    Schema::table('classifieds', function(Blueprint $table) {
        $table->dropForeign('classifieds_category_id_foreign');
    });
}

模型是:

Classified模特:

class Classified extends Model
{
   protected $table = 'classifieds';

   protected $fillable = ['title', 'category_id', 'description', 'price'];

   protected $hidden = [];

   public function category(){
       return $this->belongsTo('App\Category');

   }
}

Category 模型:

class Category extends Model
{
   protected $table = 'categories';
   protected $fillable = ['name'];

   protected $hidden = [];

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

控制器中的store方法是这样定义的:

public function store(Request $request)
{
    $title = $request->input('title');
    $category_id = $request->input('category_id');
    $description = $request->input('description');
    $price = $request->input('price');

    Classified::create([
            'title' =>  $this->title,
            'category_id' => $this->category_id,
            'description' => $this->description,
            'price' => $this->price
    ]);

    return \Redirect::route('classifieds.index')
        ->with('message', 'Ad created');
}

我在数据库设置中的错误是什么?

【问题讨论】:

    标签: php mysql laravel laravel-5.2


    【解决方案1】:

    当您尝试保存 Classified 并使用 Category 表中尚不存在的类别 ID 分配外键时,会发生这种情况。

    如果您还没有外国 ID,只需将其保留为 null 并确保在迁移时执行此操作以允许 null 值;

     public function up()
    {
        Schema::table('classifieds', function(Blueprint $table) {
            $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('set null');
        });
    }
    
    public function down()
    {
        Schema::table('classifieds', function(Blueprint $table) {
            $table->dropForeign('classifieds_category_id_foreign');
        });
    }
    

    【讨论】:

    • 嗨。我仍然得到相同的Integrity constraint violation 错误:SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(myclassified.classifieds,CONSTRAINT classifieds_category_id_foreign FOREIGN KEY( category_id) 参考 categories (id) 删除设置为 NULL)
    • 您是否再次运行迁移?
    • @byteseeker 我错了。我不认为你可以添加不存在的外键 id
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2014-03-12
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    相关资源
    最近更新 更多