Saturday, April 15, 2017

LARAVEL 5.4 SQL "column not found" while seeding

I wonder if there is an "SQL cache" or something enabled by default in Laravel 5.4

I'm having this sql issue while running artisan db:seed :

SQLSTATE[42703]: Undefined column: 7 ERROR:  column « category »  
of relation « projects » doesn't exists                                 
LINE 1: insert into "projects" ("category", "user_id","updated_at",...

Column category did exist before but I renamed it to category_id

create_project_table migration :

class CreateProjectsTable extends Migration
{
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->string('title')->unique();
            $table->text('content');
            $table->boolean('published')->default('false');
            $table->integer('views_count')->nullable();
            $table->integer('likes_count')->nullable();
            $table->timestamps();

            $table->foreign('category_id')
                  ->references('id')
                  ->on('project_categories')
                  ->onDelete('restrict');
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
        });
    }

UserTable seeder :

class UsersTableSeeder extends Seeder
{

    public function run()
    {
        $users = factory(App\User::class, 50)
               ->create()
               ->each(function ($u) {
                   $u->projects()->save(factory(App\Project::class)->make());
        });
    }
}

ProjectFactory :

$factory->define(App\Project::class, function (Faker\Generator $faker) {

    return [
        'title' => $faker->text($maxNbChars = 50),
        'content' => $faker->text($maxNbChars = 300),
        'category_id' => $faker->numberBetween(1,50),
        'published' => $faker->boolean($chanceOfGettingTrue = 50),
        'user_id' => $faker->numberBetween(1,50),
        'views_count' => $faker->numberBetween(1,200),
        'likes_count' => $faker->numberBetween(1,200)
    ];
});

ps : I already ran :

php artisan migrate:refresh

php artisan cache:clear



via C Taque

Advertisement