0
votes

i have reply_qs table and postqs table.Postqs_id is foreign key in reply_qs table.when i tried to save the reply_qs form data in database,its showed this error.

ERROR:

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))

how i can solve it? and please explain why im getting this error.

reply_qs model :

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

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

postqs model :

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

store function:

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

migration:

   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;');
1
Please add tags for the language and frameworks used since this isn’t just SQL directly to MySQL - Sami Kuhmonen
Crazy question, but are you really getting data for postqs_id? Or is that NULL? - cwallenpoole
@cwallenpoole it is not null.. - kavi
You have defined field as not null only in one table. You have to set those the same: both related fields (id from reply_qs and postqs_id from Postqs need to be of same kind - not null. - Tpojka
@Tpojka its already in NOT NULL in the table. - kavi

1 Answers

0
votes

You'll need to change your relationship.

In your reply_qs model:

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