Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Conversion failure when using model::with in Laravel

I'm using Laravel eloquent ORM and I have some relationships defined as such:

class Item extends Model
{
    protected $table = 'IV00102';
    protected $primaryKey = 'ITEMNMBR';
    protected $trimmableAttributes = ['ITEMNMBR', 'PRIMVNDR'];

    public function backorderQuantities(){
        return $this->hasOne(BackorderQuantity::class, 'ITEMNMBR', 'ITEMNMBR')->where('SOPTYPE', 5);
    }

}

and the relational model as :

class BackorderQuantity extends Model
{

    protected $table = 'SOP10200';
    protected $primaryKey = 'ITEMNMBR';
    protected $trimmableAttributes = ['ITEMNMBR', 'SOPNUMBE', 'SOPTYPE', 'QUANTITY', 'QTYREMAI'];

    public function item(){
        return $this->belongsTo(Item::class, 'ITEMNMBR', 'ITEMNMBR');
    }

}

The problem arises when I try and fetch the data with the defined relationship:

Item::with('backorderQuantities')->where('PRIMVNDR', Auth::user()->vendor_id)

Doing so results in this Error:

[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'X123RECORD231' to data type int

I can make this work without eagerloading doing 2 queries and then looping through and checking the relationship like so:

foreach ($objects as $view) {
            if($view->backorderQuantities){
                // do things
            }  
        }

but this presents a number of issues for me and is more like a bandaid than a legitimate fix. I would like to know if there is a fix to this or if the compatibility between an SQL server and MSSQL server just won't fly.

like image 221
BRose Avatar asked Jul 22 '20 22:07

BRose


1 Answers

This bug has been resolved. I'm still fairly certain it was a compatibility issue, but this is one of those weird instances where a code somehow resolves itself. I will update this answer if/when I isolate and come across the problem again in the future.

like image 130
BRose Avatar answered Oct 31 '22 17:10

BRose