Laravel Eloquent first或Create无法正常工作



我已经习惯了,但今天这个问题让我变得软弱。。;;

class Market {
// ..
public function ttl()
{
return $this->ttlRelation()->firstOrCreate(
['market_id' => $this->id],
['tier' => 0, 'direction'=>0]
);
}
}

Market模型具有一个TTL模型。我知道firstOrCreate方法找到一个项作为第一个给定的数组,如果它不存在,创建一个新的作为persistent,则返回它

此外,它的质量分配,所以我在ttl模型上填充了$fillable性质。。

class TradingTacticalLayer extends Model
{
public $timestamps = false;
protected $fillable = ['direction', 'tier'];
}

我收到SQLSTATE[HY000]: General error: 1364 Field 'direction' doesn't have a default value (SQL: insert into "trading_tactical_layer_test" ("tier", "market_id") values (0, 1))消息。我不明白为什么这个方法不能正确地填充插入字段列表。我预计,如果我将$fillable属性编辑为['direction'],SQL将内爆("方向"(为插入字段,但它不会。

一般来说,根据我的经验,我只是将这些字段设置为可以为null或手动设置默认值。这个时候,我想知道为什么会发生这种奇怪的事情,我做错了什么。

很可能,optimize:clear解决了这个问题。

我仍然不知道是什么导致了这个错误,但如果您遇到$fillable属性和插入字段列表不匹配的情况,optimize:clear无论如何都是一个选项。。

最新更新