`hasone` 关联为什么生成 `is not null` 的 SQL 语句

    /**
     * 关联订单
     *
     */
    public function order()
    {
        return $this->hasOne(Order::class,'order_uuid', 'order_uuid');
    }
select
  *
from
  `orders`
where
  `orders`.`order_uuid` = '3321718356437'
  and `orders`.`order_uuid` is not null
  and `orders`.`delete_time` is null
limit
  1

这样做有什么意义

我未曾珍惜的,我不再拥有?‍?️
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 5

我的hasOne也不会加is not null所以我猜想这不是hasOne的问题,您可以检查下是否有别的代码影响

2年前 评论

uuid不为null给你筛选了,delete_time应该是软删除,为空则是没删除的??

2年前 评论
DogLoML

用了软删除

2年前 评论

找到原因了,文件地址:

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

67行:

public function addConstraints()
    {
        if (static::$constraints) {
            $this->query->where($this->foreignKey, '=', $this->getParentKey());

            $this->query->whereNotNull($this->foreignKey);
        }
    }
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!