From 02827a66700f3a1d05ffb4065d1e664da94ee49e Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 10 Sep 2020 14:48:09 +1200 Subject: [PATCH] FIX Problem with direct compare in SELECT statement causing incompatibility with SQL Server database Co-authored-by: Tomas Bilek --- src/ORM/FieldType/DBForeignKey.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ORM/FieldType/DBForeignKey.php b/src/ORM/FieldType/DBForeignKey.php index d8afaab8c..d3ea99352 100644 --- a/src/ORM/FieldType/DBForeignKey.php +++ b/src/ORM/FieldType/DBForeignKey.php @@ -96,11 +96,10 @@ class DBForeignKey extends DBInt // Remove distinct. Applying distinct shouldn't be required provided relations are not applied. $dataQuery->setDistinct(false); - $dataQuery->setSelect(['over_threshold' => 'count(*) > ' . (int) $threshold]); + $dataQuery->setSelect(['over_threshold' => '(CASE WHEN count(*) > ' . (int)$threshold . ' THEN 1 ELSE 0 END)']); $result = $dataQuery->execute()->column('over_threshold'); - // Checking for 't' supports PostgreSQL before silverstripe/postgresql@2.2 - $overThreshold = !empty($result) && ($result[0] === 't' || (int) $result[0] === 1); + $overThreshold = !empty($result) && ((int) $result[0] === 1); static::$foreignListCache[$hasOneClass] = [ 'overThreshold' => $overThreshold,