字段没有默认值错误

huangapple go评论66阅读模式
英文:

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 = [
        &#39;first name&#39;,
        &#39;last name&#39;,
        &#39;phoneNumber&#39;,
        &#39;email&#39;,
        &#39;password&#39;,
        &#39;image&#39;,
        &#39;reg_token&#39;
    ];

I Have this migration and factory

&lt;?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(&#39;users&#39;, function (Blueprint $table) {
            $table-&gt;id();
            $table-&gt;string(&#39;firstname&#39;);
            $table-&gt;string(&#39;lastname&#39;);
            $table-&gt;string(&#39;email&#39;)-&gt;unique();
            $table-&gt;string(&#39;profile_picture&#39;);
            $table-&gt;timestamp(&#39;email_verified_at&#39;)-&gt;nullable();
            $table-&gt;string(&#39;password&#39;);
            $table-&gt;timestamps();
            $table-&gt;string(&#39;phoneNumber&#39;);
            $table-&gt;string(&#39;reg_token&#39;);
            $table-&gt;string(&#39;person_type&#39;)-&gt;nullable();
            $table-&gt;unsignedBigInteger(&#39;person_id&#39;)-&gt;nullable();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists(&#39;users&#39;);
    }
};
&lt;?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory&lt;\App\Models\User&gt;
 */
class UserFactory extends Factory
{
    /**
     * Define the model&#39;s default state.
     *
     * @return array&lt;string, mixed&gt;
     */
    public function definition(): array
    {
        return [
            &#39;firstname&#39; =&gt; fake()-&gt;name(),
            &#39;lastname&#39; =&gt; fake()-&gt;name(),
            &#39;email&#39; =&gt; fake()-&gt;unique()-&gt;safeEmail(),
            &#39;profile_picture&#39; =&gt; fake()-&gt;imageUrl(),
            &#39;email_verified_at&#39; =&gt; now(),
            &#39;password&#39; =&gt; &#39;$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi&#39;,
            &#39;phoneNumber&#39; =&gt; &quot;0123456789&quot;,
            &#39;reg_token&#39; =&gt; Str::random(10),
        ];
    }

    /**
     * Indicate that the model&#39;s email address should be unverified.
     */
    public function unverified(): static
    {
        return $this-&gt;state(fn(array $attributes) =&gt; [
            &#39;email_verified_at&#39; =&gt; 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 &#39;firstname&#39; doesn&#39;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 = [
        &#39;firstname&#39;,
        &#39;lastname&#39;,
        &#39;phoneNumber&#39;,
        &#39;email&#39;,
        &#39;password&#39;,
        &#39;image&#39;,
        &#39;reg_token&#39;
    ];

huangapple
  • 本文由 发表于 2023年7月6日 22:23:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定