Laravel SQLSTATE[HY000]:一般错误:1364 字段“id”没有默认值

ST8*_*T80 0 php mysql laravel

我在 Laravel 应用程序上工作了一段时间,但是当我php artisan migrate现在运行 时,突然间我总是收到错误消息:

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `migrations` (`migration`, `batch`) values (2019_06_05_080701_create_departments_table, 22))
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这种情况,因为以前从未发生过。当我尝试迁移特定的迁移时,我遇到了同样的错误,那么这里发生了什么?

迁移如下所示:

public function up()
{
    Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('company_id');
        $table->string('name');
        $table->string('p_number')->nullable();
        $table->string('address')->nullable();
        $table->string('zipcode')->nullable();
        $table->string('city')->nullable();
        $table->string('contact_name')->nullable();
        $table->string('contact_email')->nullable();
        $table->string('contact_phone')->nullable();

        $table->timestamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

的结果 SHOW COLUMNS FROM migrations;

在此处输入图片说明

Rah*_*hul 5

触发此查询应该适用于自动递增,这就是 id 的值没有自动递增的原因

ALTER TABLE migrations MODIFY id INTEGER NOT NULL AUTO_INCREMENT;
Run Code Online (Sandbox Code Playgroud)

注意:如果有 0 的 id,那么您应该将其更改为其他一些唯一的 id 值,然后通过查询获取 id 的最大值并将 autoincrement 设置为 +1

SET @new_index = (SELECT MAX(id) FROM migrations );
SET @sql = CONCAT('ALTER TABLE migrations AUTO_INCREMENT = ', @new_index);
PREPARE st FROM @sql;
EXECUTE st;
Run Code Online (Sandbox Code Playgroud)

参考