Closed
Description
- Laravel Version: 8.1.0
- PHP Version: 7.4.3
- Database Driver & Version: Microsoft ODBC Driver 17 for SQL Server + SQLSRV 5.8.1
Description:
Laravel fails to eager load morphTo()
relationship when morphed model's $keyType = 'string'
, but key itself is numeric.
Steps To Reproduce:
From example project https://github.com/saulens22/laravel8mssql/blob/master/tests/Feature/LaravelMssqlTest.php
$thing1 = Thing::firstOrCreate(['thekey' => 'xyz', 'data' => 'Lorem ipsum']);
$comment1 = $thing1->comments()->create([
'comment' => 'A new comment.',
]);
// works OK
$collection = Comment::with('commentable')->get();
$thing2 = Thing::firstOrCreate(['thekey' => '123', 'data' => 'Lorem ipsum']);
$comment2 = $thing2->comments()->create([
'comment' => 'Second comment.',
]);
// fails on next line
$collection = Comment::with('commentable')->get();
Error:
Illuminate\Database\QueryException
SQLSTATE[22018]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting the nvarchar value 'xyz' to data type int. (SQL: select * from [things] where [things].[thekey] in (xyz, 123))
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667▕ // If an exception occurs when attempting to run a query, we'll format the error
668▕ // message to include the bindings with SQL, which will make this exception a
669▕ // lot more helpful to the developer instead of just the database's errors.
670▕ catch (Exception $e) {
➜ 671▕ throw new QueryException(
672▕ $query, $this->prepareBindings($bindings), $e
673▕ );
674▕ }
675▕
+16 vendor frames
17 tests/Feature/LaravelMssqlTest.php:33
Illuminate\Database\Eloquent\Builder::get()