簡體   English   中英

Laravel 5.2 - 違反完整性約束:1452 無法添加或更新子行:外鍵約束失敗

[英]Laravel 5.2 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

這個問題已經被問過很多次了,我瀏覽了所有的答案,但沒有一個能解決我遇到的錯誤。

我正在使用 Laravel 5.2

我有 2 個表 - 分類和類別。 當我想創建一個分類時,我收到錯誤消息:

SQLSTATE [23000]:完整性約束違規:1452不能添加或更新子行,外鍵約束失敗( myclassifiedclassifieds ,約束classifieds_category_id_foreign外鍵( category_id )參考categoriesid ))

遷移文件定義如下:

對於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');
}

我在數據庫設置中的錯誤是什么?

當您嘗試保存Classified並使用Category表中尚不存在的 category id 分配外鍵時,就會發生這種情況。

如果您還沒有外部 ID,只需將其保留為 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');
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM