我在使用laravel 9.0.1迁移时遇到了错误。
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();
$table->bigInteger('height', 20)->nullable();
$table->bigInteger('age', 10)->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();
});
}
Error is: SQLSTATE[42000]: Syntax Error or access violation: 1075表定义错误;auto列只能有一个,并且必须定义为键。
我该如何解决这个问题?
改变你的height
&age
列到
...
$table->integer('height')->nullable();
$table->integer('age')->nullable();
原因:
$table->bigInteger('height', 20)->nullable();
$table->bigInteger('age', 10)->nullable();
//它的SQL转换如下
`height` int null auto_increment primary key,
`age` int null auto_increment primary key,"
//因此不能多次添加主键