2017-09-23 91 views
0

我有reply_qs表和postqs表.Postqs_id是reply_qs表中的外鍵。當我嘗試將reply_qs表單數據保存到數據庫中時,它顯示此錯誤。完整性約束衝突:1048列'postqs_id'不能爲空

錯誤:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'postqs_id' cannot be null (SQL: insert into reply_qs (reply , postqs_id , updated_at , created_at) values (dd, , 2017-09-23 02:54:16, 2017-09-23 02:54:16))

我怎麼能解決呢?並請解釋爲什麼我得到這個錯誤。

reply_qs模型:

protected $table = 'reply_qs'; 
protected $fillable = ['reply']; 

public function postqs(){ 
    return $this->hasMany('App\Postqs', 'postqs_id', 'id'); 
    } 

postqs模型:

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

存儲功能:

public function store(Request $request) { 
    $reply = new Reply_qs(); 
    $reply->reply = $request->get('reply'); 
    $reply->postqs_id = $request->get('postqs_id'); 
    $reply->save(); 

遷移:

Schema::create('reply_qs', function (Blueprint $table) { 
     $table->increments('id')->unique(); 
     $table->text('reply'); 
     $table->timestamps('date'); 
     }); 

    DB::statement('SET FOREIGN_KEY_CHECKS=0;'); 
    Schema::table('reply_qs',function(Blueprint $table) 
    { 
    $table->integer('postqs_id')->unsigned(); 
    $table->foreign('postqs_id')->references('id')->on('postqs') - 
    >onDelete('cascade')->onUpdate('cascade'); 
    }); 

    DB::statement('SET FOREIGN_KEY_CHECKS=1;'); 
+0

請添加對,因爲這所使用的語言和框架標籤不只是SQL直接到MySQL –

+0

瘋狂的問題,但你真正得到的數據'postqs_id'?或者是那個NULL? – cwallenpoole

+0

@cwallenpoole它不是空的。 – kavi

回答

0

你需要改變你的關係。

在你reply_qs型號:

public function postqs(){ 
    return $this->belongsTo('App\Postqs', 'postqs_id', 'id'); 
} 
+0

是的,我試過仍然不工作! – kavi

相關問題