Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nahbyr's avatar

Pivot seeder: Unknown column '' in 'field list'

Hi

I'm trying to seed a pivot table, but it keeps adding an empty column name and value to the query. I have no idea where it's coming from. The pivot has extra columns and I have tried to remove the array to see if that was the issue, but it persists even without.

Error when I execute php artisan migrate:fresh --seed:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'field list' (Connection: mysql, SQL: insert into `contact_vehicle` (``, `accessory_id`, `contact_id`, `created_at`, `created_by`, `discount`, `edited_by`, `status_id`, `takeover_id`, `total_price`, `updated_at`) values (?, 10, 13, 2023-10-24 10:21:47, 13, 1012.95, 10, 10, 3, 80175.14, 2023-10-24 10:21:47), (?, 10, 26, 2023-10-24 10:21:47, 13, 1012.95, 10, 10, 3, 80175.14, 2023-10-24 10:21:47), (?, 10, 35, 2023-10-24 10:21:47, 13, 1012.95, 10, 10, 3, 80175.14, 2023-10-24 10:21:47))

Relationship in Vehicle model:

    public function contacts() {
        return $this->belongsToMany(Contact::class)
            ->withPivot('takeover_id', 'accessory_id', 'status_id', 'edited_by', 'created_by', 'total_price', 'discount')
            ->withTimestamps();
    }

Relationship in Contact model:

    public function vehicles() {
        return $this->belongsToMany(Vehicle::class)
            ->withPivot('takeover_id', 'accessory_id', 'status_id', 'edited_by', 'created_by', 'total_price', 'discount')
            ->withTimestamps();
    }

Seeder for Vehicle (Contacts have been created at that point)

    public function run(): void
    {
        $contacts = Contact::all();

        Vehicle::factory(100)->create()->each(function($vehicle) use($contacts) {
            $vehicle->contacts()->attach($contacts->random(3),
                [
                'takeover_id' => Takeover::pluck('id')[fake()->numberBetween(0,Takeover::count()-1)],
                'accessory_id' => Accessory::pluck('id')[fake()->numberBetween(0,Accessory::count()-1)],
                'status_id' => Status::pluck('id')[fake()->numberBetween(0,Status::count()-1)],
                'edited_by' => User::pluck('id')[fake()->numberBetween(0,User::count()-1)],
                'created_by' => User::pluck('id')[fake()->numberBetween(0,User::count()-1)],
                'total_price' => fake()->randomFloat(2, 1000, 90000),
                'discount' => fake()->randomFloat(2, 1000, 5000),
            ]);
        });
    }
0 likes
4 replies
LaryAI's avatar
Level 58

The error message suggests that there is an unknown column in the field list of the insert query. Looking at the seeder code, it seems that the issue is with the attach method in the pivot table relationship.

The attach method expects an array of IDs as the first argument, but in the code provided, it is passing a collection of contacts. To fix this, you can use the pluck method on the contacts collection to get an array of contact IDs.

Here's the updated seeder code:

public function run(): void
{
    $contacts = Contact::all();

    Vehicle::factory(100)->create()->each(function($vehicle) use($contacts) {
        $vehicle->contacts()->attach($contacts->pluck('id')->random(3), [
            'takeover_id' => Takeover::pluck('id')->random(),
            'accessory_id' => Accessory::pluck('id')->random(),
            'status_id' => Status::pluck('id')->random(),
            'edited_by' => User::pluck('id')->random(),
            'created_by' => User::pluck('id')->random(),
            'total_price' => faker()->randomFloat(2, 1000, 90000),
            'discount' => faker()->randomFloat(2, 1000, 5000),
        ]);
    });
}

In the updated code, the pluck method is used to get an array of contact IDs, and then the random method is called on the array to select three random contact IDs.

Make sure to import the necessary classes at the top of the seeder file:

use App\Models\Contact;
use App\Models\Vehicle;
use App\Models\Takeover;
use App\Models\Accessory;
use App\Models\Status;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

Remember to replace the App\Models namespace with the correct namespace for your models.

This should resolve the issue with the unknown column in the field list error.

Nahbyr's avatar

I have just tried adding an array_filter around my array. Just in case. It did not solve it. So the empty value gets added to the array at a later stage.

Nahbyr's avatar
Nahbyr
OP
Best Answer
Level 2

For those interested: the ID of the model I was attaching on was empty. And even when I had the ID, it would not know the column name it was supposed to reference in the pivot.

So, the solution:

Model relationship definition WITH foreignPivotKey and relatedPivotKey defined:

    public function contacts() {
        return $this->belongsToMany(Contact::class, 'contact_vehicle', 'vehicle_id', 'contact_id')
            ->withPivot('takeover_id', 'accessory_id', 'status_id', 'edited_by', 'created_by', 'total_price', 'discount')
            ->withTimestamps();
    }

And the updated seeder file with a refetch of the newly made models and then iterating over them to attach:

        Vehicle::factory(100)->create();
        $vehicles = Vehicle::all();
        $contacts = Contact::all();
        foreach ($vehicles as $vehicle) {
            $vehicle->contacts()->attach($contacts->pluck('id')->random(3),
                [
                    'takeover_id' => Takeover::pluck('id')[fake()->numberBetween(0,Takeover::count()-1)],
                    'accessory_id' => Accessory::pluck('id')[fake()->numberBetween(0,Accessory::count()-1)],
                    'status_id' => Status::pluck('id')[fake()->numberBetween(0,Status::count()-1)],
                    'edited_by' => User::pluck('id')[fake()->numberBetween(0,User::count()-1)],
                    'created_by' => User::pluck('id')[fake()->numberBetween(0,User::count()-1)],
                    'total_price' => fake()->randomFloat(2, 0, 90000),
                    'discount' => fake()->randomFloat(2, 0, 5000),
                ]);
        }

Please or to participate in this conversation.