这里是我的UserFactory:
<?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,
]);
最后,我得到了这个错误,只在staging环境中(而不是在本地环境中):
Illuminate\Database\QueryException:SQLSTATE[HY000]:一般错误:1364 1364 Field 'firstname' doesn't have a default value(SQL:插入到users
(name
,email
,password
,remember_token
,isTeacher
,termsofuse_id
,privacypolicy_id
,updated_at
,created_at
)值(Kenneth Altenwerth Jr.,koch.库珀example.org,$2y$10$1B7LlTzM4iINkVI/82Pp6OHSUWBWaePpdDKMy1DRW2K02d8UYKc2S,r0WnPBrZGG,1,1,1,2021-07-29 21:12:54,2021-07-29 21:12:54))
有谁知道我该怎么解决?谢谢!
3条答案
按热度按时间h5qlskok1#
在迁移中将其设置为可空:
rdlzhqv92#
另外要注意的是,如果你为模型编写了一个构造函数,请确保将
$attributes
数组传递给父构造函数,否则所有的属性都不会传递给模型create
方法。示例
h9vpoimq3#
在我看来,你的表中没有名字列。创建它,并使它默认为空,就像其他人说的那样。