Rretzko's avatar
Level 15

Seeder error: column not found 'updated_at'

Hi - I have a weird issue seeding a pivot table that I just can't see and hoping you can help: Here's the model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SchoolTeacher extends Model
{
    protected $table = 'school_teacher';

    protected $fillable = ['school_id', 'teacher_status_type_id', 'user_id'];
}

and the migration:

Schema::create('school_teacher', function (Blueprint $table) {
            $table->foreignId('school_id')->constrained();
            $table->foreignId('user_id')->constrained();
            $table->foreignId('teacher_status_type_id')->constrained();
            $table->primary(['school_id','user_id']);
        });

and the seeder:

<?php

namespace Database\Seeders;

use App\Models\SchoolTeacher;
use Carbon\Carbon;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class SchoolTeacherSeeder extends Seeder
{
    private $seeds;

    public function __construct()
    {
        $this->buildSeeds();
    }

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        foreach($this->seeds AS $seed){

            SchoolTeacher::create(
                [
                    'school_id' => $seed[0],
                    'user_id' => $seed[1],
                    'teacher_status_type_id' => 1, //active
                ]
            );
        }
    }

    private function buildSeeds()
    {
        $this->seeds = [
            [1354,284],
            [3420,8474],
            [1856,429],
           ...
}

When I run: php artisan db:seed --class=<file_name>, I get the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (Connection: mysql, SQL: insert into `school_teacher` (`school_id`, `user_id`, `teacher_status_type_id`
, `updated_at`, `created_at`) values (1354, 284, 1, 2023-05-12 15:49:56, 2023-05-12 15:49:56))

I've tried 'php artisan cache:clear ' and 'php artisan optimize' without improvement. I don't see what I'm missing here and hope you can! Any thoughts? Thanks - Rick

0 likes
4 replies
LaryAI's avatar
Level 58

The error message suggests that the updated_at column is missing in the school_teacher table. This is because Laravel's Eloquent ORM expects the table to have created_at and updated_at columns by default. To fix this issue, you can add the timestamps() method to the migration file:

Schema::create('school_teacher', function (Blueprint $table) {
    $table->foreignId('school_id')->constrained();
    $table->foreignId('user_id')->constrained();
    $table->foreignId('teacher_status_type_id')->constrained();
    $table->primary(['school_id','user_id']);
    $table->timestamps(); // add this line
});

This will add the created_at and updated_at columns to the school_teacher table. Once you've made this change, you can run the migration again to update the table:

php artisan migrate

After that, you should be able to run the seeder without any issues.

Rretzko's avatar
Level 15

OK, I can do that to get past the issue, but pivot tables don't populate the created_at/updated_at columns, so why the default?

PovilasKorop's avatar
Level 11

@rretzko if you create a model for pivot table, you specifically need to provide to NOT enable timestamps there.

public $timestamps = false;

Don't remember public or protected.

4 likes
Rretzko's avatar
Level 15

Thanks, Povilas - As always, right on target! ( it is public, by the way)

Please or to participate in this conversation.