播种时LARAVEL 5.4 SQL“找不到列”
Posted
技术标签:
【中文标题】播种时LARAVEL 5.4 SQL“找不到列”【英文标题】:LARAVEL 5.4 SQL "column not found" while seeding 【发布时间】:2017-09-11 13:01:36 【问题描述】:我想知道 Laravel 5.4 中是否有“SQL 缓存”或默认启用的东西
我在运行 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 迁移:
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());
);
项目工厂:
$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 迁移:刷新
php 工匠缓存:清除
更新
我可以在 Tinker 中创建一个项目而不会出现此错误
【问题讨论】:
你是否在任何地方使用 Cache::remember() 函数? 嗯,不,我没用那个 【参考方案1】:已解决:
$factory->define(App\Project::class, function (Faker\Generator $faker)
...
在每个工厂定义中,如果复制粘贴,请替换类的名称。
【讨论】:
以上是关于播种时LARAVEL 5.4 SQL“找不到列”的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE [42S22]:找不到列:1054 未知列 laravel 5.1
找不到列:1054“字段列表”中的未知列“0”-Laravel-我的代码中的任何地方都没有0列