Gabotronix's avatar

Unknown column 'ìtem_size_id' in hasManyThrough relationship

Hi everybody, I'm trying to build my own e-commerce shop as training with laravel, I set up a hasManyThrough relationship with models Item, ItemOption,ItemSize and ItemColor. Isssue arises when I seed my database with dummy data, I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ìtem_size_id' in 'field list' (SQL: insert into `item_options` (`item_id`, `item_color_id`, `ìtem_size_id`, `stock`) values (1, 1, 1, 10))

This is the seed file where error happens:

<?php


use Illuminate\Database\Seeder;
use Carbon\Carbon;


class ItemOptionsSeeder extends Seeder
{
    
    
    public function run()
    {
        //blancas
        DB::table('item_options')->insert([ 'item_id' => 1, 'item_color_id' => 1, 'ìtem_size_id' => 1, 'stock' => 10 ]);
        //negras
        DB::table('item_options')->insert([ 'item_id' => 1, 'item_color_id' => 2, 'ìtem_size_id' => 2, 'stock' => 10 ]);
        //rojas
        DB::table('item_options')->insert([ 'item_id' => 1, 'item_color_id' => 3, 'ìtem_size_id' => 3, 'stock' => 10 ]);
        //verdes
        DB::table('item_options')->insert([ 'item_id' => 1, 'item_color_id' => 4, 'ìtem_size_id' => 4, 'stock' => 10 ]);

    }


}

I think I set up relationships and models correctly and I can't seem to find what's wrong with my code, let's start with My models and then migrations:

Models:

<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;


class Item extends Model
{
    
    
    protected $table = 'items';
    
    
    public function options()
    {
        return $this->hasMany(ItemOption::class);
    }

    public function sizes()
    {
        return $this->hasManyThrough(ItemSize::class, ItemOption::class, 'item_size_id', 'id');
    }

    public function colors()
    {
        return $this->hasManyThrough(ItemColor::class, ItemOption::class, 'item_color_id', 'id');
    }
}
<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;


class ItemOption extends Model
{
    
    protected $fillable = ['item_id', 'item_color_id', 'item_size_id', 'stock'];
    
    public function item()
    {
        return $this->belongsTo('App\Models\Item', 'item_id');
    }
    
    public function color()
    {
        return $this->belongsTo('App\Models\ItemColor', 'item_color_id');
    }

    public function size()
    {
        return $this->belongsTo('App\Models\ItemSize', 'item_size_id');
    }
}
<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;


class ItemSize extends Model
{
    
    
    protected $table = 'item_sizes';

    
}

<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;


class ItemColor extends Model
{
    
    
    protected $table = 'item_colors';

    
}

And migrations:

<?php


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class ItemOptions extends Migration
{
    

    public function up()
    {


        Schema::create('item_options', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('item_id')->index()->nullable();
            $table->foreign('item_id')->references('id')->on('items')->nullable();
            $table->unsignedInteger('item_size_id')->index()->nullable();
            $table->foreign('item_size_id')->references('id')->on('item_sizes')->nullable();
            $table->unsignedInteger('item_color_id')->index()->nullable();
            $table->foreign('item_color_id')->references('id')->on('item_colors')->nullable();
            $table->integer('stock');
            $table->timestamps();
        });


    }

    
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('item_options');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }


}

<?php


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class Items extends Migration
{
    

    public function up()
    {


        Schema::create('items', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image')->nullable();
            $table->string('title');
            $table->string('url')->unique();
            $table->string('slug')->unique();
            $table->text('body');
            $table->decimal('finalPrice', 5,2);
            $table->boolean('isVisible')->default(false);
            $table->boolean('isFeatured')->default(false);
            $table->boolean('beenPublished')->default(false);
            $table->boolean('scheduleForMail')->default(false);
            $table->timestamps();
        });


    }

    
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('items');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }


}

<?php


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class ItemSizes extends Migration
{
    

    public function up()
    {


        Schema::create('item_sizes', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
        });


    }

    
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('item_sizes');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }


}
<?php


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class ItemColors extends Migration
{
    

    public function up()
    {


        Schema::create('item_colors', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('colorCode');
            $table->timestamps();
        });


    }

    
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('item_colors');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }


}

Any idea what I did wrong?

0 likes
2 replies
Sinnbeck's avatar

Your migration, seeder and error would have been enough I think. :) Anyways. Check your database manually to see if the column is indeed missing

Gabotronix's avatar

Sorry guys, it was because of í instead of i ...

Please or to participate in this conversation.