在 Laravel 8 中使用工厂时字段名称没有默认值错误
Field name doesn't have default value error while using factories in Laravel 8
这是我的用户工厂:
<?php
namespace Database\Factories;
use App\User;
use Faker\Generator as Faker;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
$factory->define(User::class, function (Faker $faker) {
static $password;
return [
'firstname' => $this->faker->firstName(),
'name' => $this->faker->lastName,
'nickname' => $this->faker->userName,
'email' => $this->faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => Str::random(10),
];
});
这是我的用户模型的第一部分:
class User extends Authenticatable implements HasLocalePreference
{
use Notifiable;
protected $table = 'users';
protected $appends = ['avatar','targetLang','targetLangId','type', 'isAdmin'];
protected $casts = [
'preferences' => 'array',
];
protected $guarded= [];
protected $hidden = [
'password', 'remember_token','accessToken','refreshToken',
];
以及我调用工厂的测试:
class MoveCourseItemTest extends DuskTestCase
{
/**
* A Dusk test to move card in other block.
*
* @return void
*/
public function testMoveCard() {
// Create Teacher User
$teacher = factory(User::class)->create([
'isTeacher' => 1,
'termsofuse_id' => 1,
'privacypolicy_id' => 1,
]);
最后我得到这个错误,只在暂存环境中(不是在本地环境中):
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error:
1364 Field 'firstname' doesn't have a default value (SQL: insert into
users
(name
, email
, password
, remember_token
, isTeacher
,
termsofuse_id
, privacypolicy_id
, updated_at
, created_at
)
values (Kenneth Altenwerth Jr., koch.cooper@example.org,
yB7LlTzM4iINkVI/82Pp6OHSUWBWaePPdDKMy1DRW2K02d8UYKc2S,
r0WnPBrZGG, 1, 1, 1, 2021-07-29 21:12:54, 2021-07-29 21:12:54))
有人知道我怎么解决吗?谢谢!
在我看来,您的 table 中没有名字栏。正如其他人所说,创建它并使其默认为空。
在您的迁移中使其可为空:
$table->string('name')->nullable();
这是我的用户工厂:
<?php
namespace Database\Factories;
use App\User;
use Faker\Generator as Faker;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
$factory->define(User::class, function (Faker $faker) {
static $password;
return [
'firstname' => $this->faker->firstName(),
'name' => $this->faker->lastName,
'nickname' => $this->faker->userName,
'email' => $this->faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => Str::random(10),
];
});
这是我的用户模型的第一部分:
class User extends Authenticatable implements HasLocalePreference
{
use Notifiable;
protected $table = 'users';
protected $appends = ['avatar','targetLang','targetLangId','type', 'isAdmin'];
protected $casts = [
'preferences' => 'array',
];
protected $guarded= [];
protected $hidden = [
'password', 'remember_token','accessToken','refreshToken',
];
以及我调用工厂的测试:
class MoveCourseItemTest extends DuskTestCase
{
/**
* A Dusk test to move card in other block.
*
* @return void
*/
public function testMoveCard() {
// Create Teacher User
$teacher = factory(User::class)->create([
'isTeacher' => 1,
'termsofuse_id' => 1,
'privacypolicy_id' => 1,
]);
最后我得到这个错误,只在暂存环境中(不是在本地环境中):
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364 Field 'firstname' doesn't have a default value (SQL: insert into
users
(name
,password
,remember_token
,isTeacher
,termsofuse_id
,privacypolicy_id
,updated_at
,created_at
) values (Kenneth Altenwerth Jr., koch.cooper@example.org, yB7LlTzM4iINkVI/82Pp6OHSUWBWaePPdDKMy1DRW2K02d8UYKc2S, r0WnPBrZGG, 1, 1, 1, 2021-07-29 21:12:54, 2021-07-29 21:12:54))
有人知道我怎么解决吗?谢谢!
在我看来,您的 table 中没有名字栏。正如其他人所说,创建它并使其默认为空。
在您的迁移中使其可为空:
$table->string('name')->nullable();