hasOne()->ofMany() failing in SQL Server with Ambiguous Column Name error since update to 8.67.0 #39369
Closed
Description
- Laravel Version: 8.67.0
- PHP Version: 7.4
- Database Driver & Version: MS SQL Server 2019
Description:
I have the following relationship:
return $this->hasOne(AssignmentMark::class, 'enrolment_id', 'EnrolmentID')
->ofMany([
'assignment_id' => 'max'
],
function ($query) {
$query->join('assignments AS a', 'a.id', 'assignment_marks.assignment_id')
->where('due_date', '<=', now())
->whereNotNull('assignment_marks.grade')
->whereIn('assignment_type_id', [1, 2]);
},
'am'
)->withDefault();
Comparing the generated SQL between 8.64 and 8.67, I notice that it is quite different. The problem seems to stem from the new version adding an additional join to the assignments table, which causes the ambiguous column:
Old (working) SQL:
SELECT TOP 1 *
FROM [focus].[dbo].[assignment_marks]
INNER JOIN (SELECT Max([id]) AS [id],
[focus].[dbo].[assignment_marks].[enrolment_id]
FROM [focus].[dbo].[assignment_marks]
INNER JOIN (....
New (failing) SQL:
SELECT TOP 1
[focus].[dbo].[assignment_marks].*
FROM [focus].[dbo].[assignment_marks]
INNER JOIN
(
SELECT Max([id]) AS [id_aggregate],
[focus].[dbo].[assignment_marks].[enrolment_id]
FROM [focus].[dbo].[assignment_marks]
INNER JOIN [assignments] AS [a] ON [a].[id] = [assignment_marks].[assignment_id] /*This join causes the problem /*
INNER JOIN
(....
There are a number of other changes to the generated SQL that appear to me to be a lot more messy than in the original version (happy to reply with a side-by-side comparison if useful).
For the moment I've reverted to Laravel 8.64.
Cheers,
Martin.