LARAVEL 5.4 SQL "column not found" 播种时
LARAVEL 5.4 SQL "column not found" while seeding
我想知道在 Laravel 5.4
中是否有 "SQL cache" 或默认启用的东西
我在 运行 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 migrate:refresh
php artisan cache:clear
更新
我可以在 Tinker 中创建一个 项目 而不会出现此错误
已解决:
$factory->define(App\Project::class, function (Faker\Generator $faker){
...
}
在每个工厂定义中,如果您复制粘贴,请替换 class 的名称。
我想知道在 Laravel 5.4
中是否有 "SQL cache" 或默认启用的东西我在 运行 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 migrate:refresh
php artisan cache:clear
更新
我可以在 Tinker 中创建一个 项目 而不会出现此错误
已解决:
$factory->define(App\Project::class, function (Faker\Generator $faker){
...
}
在每个工厂定义中,如果您复制粘贴,请替换 class 的名称。