You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel9执行迁移报1075错误:仅能有一个自增列且需定义为键

错误根因

触发SQLSTATE[42000]: 1075 Incorrect table definition错误的核心原因是bigInteger()字段方法传参错误:
Laravel 中bigInteger()方法的第二个参数是是否开启自增属性的布尔值,并非字段长度。你在定义height、age字段时传入的20、10会被框架识别为布尔值true,相当于给这两个字段也加上了自增属性。而MySQL明确规定一张表只能存在一个自增列,且该列必须定义为键,你代码里已经通过$table->id()定义了默认的自增主键,多出自增列直接触发语法报错。

修复步骤
  1. 修正bigInteger字段的错误传参,整型字段在MySQL中不需要手动指定显示长度,直接移除第二个无效参数即可。修正后的完整迁移up方法代码如下:
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_goal_id')->constrained('user_goals')->onDelete('cascade');
        $table->string('full_name', 50)->nullable();
        $table->string('last_name', 50)->nullable();
        $table->enum('gender', ['Male', 'Female', 'Others'])->default('Male');
        $table->decimal('weight', 4, 2)->nullable();
        // 移除错误的第二个参数,bigint无需手动指定长度
        $table->bigInteger('height')->nullable();
        $table->bigInteger('age')->nullable();
        $table->enum('status', ['Active', 'Inactive'])->default('Active');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}
  1. 如果之前执行迁移时已经创建了部分表残留,先执行php artisan migrate:fresh清空所有表数据后重新跑迁移即可(生产环境执行该命令前请确认数据备份,避免数据丢失)。

补充提示:如果身高、年龄字段不需要存储负数,可以将bigInteger替换为unsignedBigInteger(),进一步约束字段存储合法值。

内容的提问来源于stack exchange,提问作者Sohang Tyagi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.02 02:51:31