diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index ba307ec7..3d80c2c1 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -159,11 +159,11 @@ class HasMany extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - + $localKey = $this->localKey ?: $this->parent->getPk(); return $this->query->where([ $this->foreignKey => [ 'exp', - '=' . $this->parent->getTable() . '.' . $this->parent->getPk(), + '=' . $this->parent->getTable() . '.' . $localKey, ], ])->fetchSql()->count(); }