oborrero's avatar

One to Many Relationship Query Error

I've created a one-to-many relationship between a Jobs Model and Permits model. I've recently discovered the awesome tool of Tinker so I've been using it to test my models. When I run Job::with('steps')->find(1); I get this error

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'steps.job_id' in 'where clause' (SQL: select * from `steps` where `steps`.`job_id` in (1))'

Here's my Job Model

   public function steps ()
    {
        return $this->hasMany('MyfirstApp\Step');
    }

Here's my Step Model

   public function Job ()
    {
      return $this->belongsTo('MyFirstApp\Job');
    }

I've already set up the foregin key in the Jobs Table so I'm not sure what the error can be. Any ideas?

0 likes
5 replies
EliasSoares's avatar

Could you show us your table structure?

It looks like you didn't read the error.

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'steps.job_id' in 'where clause' (SQL: select * from `steps` where `steps`.`job_id` in (1))'

Your steps table don't have a column named job_id to link your step to an specific job.

If you want to use a custom column name, just define it in your relation function as decribed in documentation:

http://laravel.com/docs/5.0/eloquent#one-to-many

" Again, you may override the conventional foreign key by passing a second argument to the hasMany method. And, like the hasOne relation, the local column may also be specified:

return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

"

1 like
oborrero's avatar

I tried switching steps to step and I got undefined method. Here's the structure:

Table Structure

EliasSoares's avatar
Level 10

Who has many who?

Your relation are describing that Job has many steps, and that Steps belongs to one Job.

Your database structure shows different. It shows that Job belongs to one Step, and Step has many Jobs.

If you want to have many steps to an job, you must remove steps_id from Jobs table, and add Jobs_id to your Steps table, and keep your relations.

oborrero's avatar

Ok, I will give that a try. I really need to get my head around database normalization, I'll do some more reading tonight. I thought I would link everything from my Jobs tables in the migration. Looks like I'll have to attach the foreign keys to the other tables.

public function up()
{
    Schema::dropIfExists('jobs');
    Schema::create('jobs', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('number');
        $table->integer('customer_id')->unsigned();
        $table->foreign('customer_id')
            ->references('id')->on('customers')
            ->onDelete('cascade');
        $table->integer('issues_id')->unsigned();
        $table->foreign('issues_id')
            ->references('id')->on('issues')
            ->onDelete('cascade');
        $table->integer('steps_id')->unsigned();
        $table->foreign('steps_id')
            ->references('id')->on('steps')
            ->onDelete('cascade');
        $table->integer('permits_id')->unsigned();
        $table->foreign('permits_id')
            ->references('id')->on('permits')
            ->onDelete('cascade');
        $table->integer('wire_centers_id')->unsigned();
        $table->foreign('wire_centers_id')
            ->references('id')->on('wire_centers')
            ->onDelete('cascade');
        $table->integer('poles_id')->unsigned();
        $table->foreign('poles_id')
            ->references('id')->on('poles')
            ->onDelete('cascade');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade');
        $table->timestamps();
    });
}
oborrero's avatar

I'll go ahead and fix my table relationships. Thank you

1 like

Please or to participate in this conversation.