英文:
field does not have a default value error
问题
以下是您要翻译的部分:
我有一个用户模型,其中包括以下可填充属性:
protected $fillable = [
'first name',
'last name',
'phoneNumber',
'email',
'password',
'image',
'reg_token'
];
我有这个迁移和工厂:
迁移文件:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* 运行迁移操作。
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->string('profile_picture');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->timestamps();
$table->string('phoneNumber');
$table->string('reg_token');
$table->string('person_type')->nullable();
$table->unsignedBigInteger('person_id')->nullable();
});
}
/**
* 回滚迁移操作。
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
工厂文件:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<App\Models\User>
*/
class UserFactory extends Factory
{
/**
* 定义模型的默认状态。
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'firstname' => fake()->name(),
'lastname' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'profile_picture' => fake()->imageUrl(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
'phoneNumber' => "0123456789",
'reg_token' => Str::random(10),
];
}
/**
* 指示模型的电子邮件地址应为未验证状态。
*/
public function unverified(): static
{
return $this->state(fn(array $attributes) => [
'email_verified_at' => null,
]);
}
}
当我运行 php artisan migrate:refresh --seed
时,我遇到了以下错误,指出字段 firstname
没有默认值:
我理解这个错误是因为 MySQL 不允许在没有默认值的情况下插入或更新空字段,但在我的代码中,我没有尝试在任何地方插入空的 firstname
。
这是错误消息:
SQLSTATE[HY000]: General error: 1364 Field 'firstname' doesn't have a default value (Connection: mysql, SQL: insert into `users` (`updated_at`, `created_at`) values (2023-07-06 14:12:05, 2023-07-06 14:12:05))
英文:
I have a User Model with these fillable
protected $fillable = [
'first name',
'last name',
'phoneNumber',
'email',
'password',
'image',
'reg_token'
];
I Have this migration and factory
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->string('profile_picture');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->timestamps();
$table->string('phoneNumber');
$table->string('reg_token');
$table->string('person_type')->nullable();
$table->unsignedBigInteger('person_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
*/
class UserFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'firstname' => fake()->name(),
'lastname' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'profile_picture' => fake()->imageUrl(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
'phoneNumber' => "0123456789",
'reg_token' => Str::random(10),
];
}
/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn(array $attributes) => [
'email_verified_at' => null,
]);
}
}
When I run php artisan migrate:refresh --seed
I encounter this error saying that the field firstname does not have a default value
My understanding is that this error occurs because mySql does not allow inserting or updating an empty field when it does not have a default value
But in my code I'm not trying to insert an empty firstname anywhere
this is the error message
SQLSTATE[HY000]: General error: 1364 Field 'firstname' doesn't have a default value (Connection: mysql, SQL: insert into `users` (`updated_at`, `created_at`) values (2023-07-06 14:12:05, 2023-07-06 14:12:05))
答案1
得分: 3
在 `$fillable` 数组中,你应该将列名设置为与数据库中的完全相同:
protected $fillable = [
'firstname',
'lastname',
'phoneNumber',
'email',
'password',
'image',
'reg_token'
];
英文:
in $fillable
array, you should set column names exactly as they are in db:
protected $fillable = [
'firstname',
'lastname',
'phoneNumber',
'email',
'password',
'image',
'reg_token'
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论