LARAVEL 5.4 SQL"列未找到"播种时

时间:2017-04-15 11:39:20

标签: php laravel laravel-5 laravel-seeding

我想知道是否有一个" SQL缓存"或者在Laravel 5.4中默认启用的东西

我在运行artisan db:seed:

时遇到了这个sql问题
SQLSTATE[42703]: Undefined column: 7 ERROR:  column « category »  
of relation « projects » doesn't exists                                 
LINE 1: insert into "projects" ("category", "user_id","updated_at",...

之前确实存在列类别,但我将其重命名为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播种机:

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: 我已经跑了:

php artisan migrate:refresh

php artisan cache:clear

更新

我可以在没有此错误的Tinker中创建项目

1 个答案:

答案 0 :(得分:0)

解决:

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

在每个工厂定义中,如果复制粘贴,请替换该类的名称。