0

I am building a training web application to track associate training and certifications. I have created an Associate model that used a secondary DB connection to another database that has its information generated by another application so I have no control over the structure of the data. The associates table uses the associate's number as primary key and not an auto incremented ID. I have created a table to keep track of every training/certification course that take place. I created a many to many relationship between the Associate and the Course but when trying to add a record to the pivot table I am running an error.

"SQLSTATE[42S02]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'associate_course'. (SQL: insert into [associate_course] ([associate_id], [course_id], [created_at], [updated_at]) values (0000, 1, 2020-01-31 18:36:56.390, 2020-01-31 18:36:56.390))",

Here is the function that is called to create a record in the pivot table (where the error occurs)

 public function trained(Course $course, Request $request) {

        $request->validate([
            'associates' => 'required'
        ]);

        $associates = explode(',', $request->associates);

        foreach($associates as $associate_number) {
            $associate = Associate::where('NUMBER', $associate_number)->first();

            $course->associates()->attach($associate);
        } 
    }

Here is my Associate model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Associate extends Model
{
    protected $connection = 'SHOPFLOOR';
    protected $table = 'USERS';

    public function getRouteKeyName()
    {
        return 'NUMBER';
    }

    public function courses()
    {
        return $this->belongsToMany(Course::class, 'associate_course', 'associate_id', 'course_id', 'NUMBER', 'id')->withTimestamps();
    }
}

and here is my Course model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    protected $fillable = ['course_type_id', 'name', 'date', 'expires', 'expires_at', 'notification_threshold'];

    public function type() {
        return $this->belongsTo(CourseType::class);
    }

    public function associates() {
        return $this->belongsToMany(Associate::class, 'associate_course', 'course_id', 'associate_id', 'id', 'NUMBER')->withTimestamps();
    }
}

I tried copying the sql from the error and running it on the database and it does insert the row into the database so that leads me to believe it's something with my Laravel configuration?

Can I get some assistance in fixing this issue?

2 Answers 2

0

It's hard to say without seeing your DB structure, Error says clearly that table specified in query is not found. Most probably is that you forgot to specify schema in the statement. IMHO code that you showed is not really related to the problem.

2
  • I agree with @AJ, it looks like your database it's trying to find associate_course and not being able to found it.
    – Brand Guy
    Feb 1, 2020 at 0:38
  • I ended up fixing the issue. Before I get to the solution as I stated in my initial post I could copy the sql that I was given in the error and run it in my DB client and it would work. I fixed the problem by specifying the DB Schema in my relation definitions. For example return $this->belongsToMany(Associate::class, 'TRAINING.dbo.associate_course', 'course_id', 'associate_id', 'id', 'NUMBER')->withTimestamps(); Feb 3, 2020 at 1:44
0

There is a bug with the Laravel sqlsrv driver. I needed to specify the full schema to get it working. In my case this was TRAINING.dbo.associate_course.

So relationships look like this

Associate.php

public function courses()
    {
        return $this->belongsToMany(Course::class, 'TRAINING.dbo.associate_course', 'associate_id', 'course_id', 'NUMBER', 'id')->withTimestamps();
    }

Course.php

public function associates() {
        return $this->belongsToMany(Associate::class, 'TRAINING.dbo.associate_course', 'course_id', 'associate_id', 'id', 'NUMBER')->withTimestamps();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.