Q:

How to fix "Field {$foreign_key} doesn't have a default value" error when updating a belongsTo relationship with associate method

I have created a eloquent relationship "one to one" between two models TokenMatch model and Match model but when I tried to associate TokenMatch to a Match I have an error:
"Field 'tokenmatch_id' doesn't have a default value (SQL: insert into `matches` (`id`, `updated_at`, `created_at`) values (, 2019-04-22 08:55:51, 2019-04-22 08:55:51))
"

My code issue:
$match = new Match();
$tokenmatch = TokenMatch::find(1);
$match->token()->associate($tokenmatch)->save();

class Match
class Match extends Model
{

    public function token()
    {
         return $this->belongsTo('App\TokenMatch' , 'id', 'tokenmatch_id');
    }
}

class TokenMatch
class TokenMatch extends Model
{
    protected  $table = 'tokensmatch';

    public function match()
    {
        return $this->hasOne('App\Match','tokenmatch_id');
    }
}

matches Table
 Schema::create('matches', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->boolean('isFinished')->nullable();
            $table->boolean('isWon')->nullable();
            $table->unsignedBigInteger('tokenmatch_id');
            $table->foreign('tokenmatch_id')->references('id')->on('tokensmatch');
            $table->timestamp('created_at')->default(\Illuminate\Support\Facades\DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at')->default(\Illuminate\Support\Facades\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

        });

tokensmatch Table
 Schema::create('tokensmatch', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->boolean('isUsed')->default(false);
            $table->string('token', 15)->unique();
            $table->dateTimeTz('expiryDate')->nullable();
            $table->boolean('isValid')->default(true);
        });

So I expected that on field "tokenmatch_id" of table Matches is not null when I save the new model match 
...
$match->token()->associate($tokenmatch)->save();
...

A:

I think there is an issue with the relationship you defined in Match model. It should be :
public function token()
{
     return $this->belongsTo('App\TokenMatch' ,'tokenmatch_id', 'id');
}

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

