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

58 views Asked by At

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))

As you can see, it claims there's an empty array key in there that I did not put in.

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();
    }

Contact model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use HasFactory;

    public function editor() {
        return $this->belongsTo(User::class, 'edited_by')->withTimestamps();
    }

    public function creator() {
        return $this->belongsTo(User::class, 'created_by')->withTimestamps();
    }

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

    public function contact_remarks() {
        return $this->belongsToMany(Remark::class, 'contact_remark_vehicle', 'contact_id', 'remark_id')
            ->withPivot('vehicle_id', 'status_id', 'edited_by', 'created_by')
            ->withTimestamps();
    }

    public function remark_contacts() {
        return $this->belongsToMany(Vehicle::class, 'contact_remark_vehicle', 'contact_id', 'vehicle_id')
            ->withPivot('remark_id', 'status_id', 'edited_by', 'created_by')
            ->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),
            ]);
        });
    }
1

There are 1 answers

0
Nahbyr On

So, I have no idea if it is supposed to be fixed this way, but I was able to deduce the origin of the empty column name. It is supposed to be the column name of the origin model's foreign key in the pivot table. So in this case that would be vehicle_id.

To force the column name I had to resort to adding it in the relationship like so:

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

Also, at the point I was iterating over the Vehicles it had not yet saved them. So they had no ID. I also had to switch that to save them first, grab them again with a $vehicles = Vehicle::all(); and then iterate over the collection:

        Vehicle::factory(100)->create();
        $vehicles = Vehicle::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),
                ]);
        }