SQLSTATE[42000]:语法错误或访问冲突:1068 定义了多个主键

问题描述 投票:0回答:1

我正在尝试将我的应用程序切换为使用 Uuid,但我的迁移在事件表上失败。

SQLSTATE[42000]:语法错误或访问冲突:1068 定义了多个主键(SQL:更改表

events
添加主键
events_id_primary
id
))

Schema::create('events', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('title', 255);
        $table->integer('join_id', 6)->unique();
        $table->string('image')->nullable();
        $table->string('location')->nullable();
        $table->text('description')->nullable();
        $table->timestamp('event_date')->nullable();
        $table->timestamp('event_time')->nullable();
        $table->foreignUuid('user_id')->nullable()->index();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->softDeletes();
        $table->timestamps();
    });


Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('name')->nullable();
        $table->string('email')->unique();
        $table->string('username')->nullable();
        $table->string('avatar')->nullable();
        $table->string('phone_number')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

非常感谢任何帮助!

php laravel laravel-8 database-migration mysql-error-1068
1个回答
3
投票

当您检查

integer()
的文档时,您会发现第二个参数不是用于大小,而是用于字段的自动增量/主字段。整数类型本身就是一个大小(tinyInteger、smallInteger、bigInteger,...)。
6
转换为布尔值返回 true。

/**
     * Create a new integer (4-byte) column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Database\Schema\ColumnDefinition
     */
    public function integer($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
    }

你只需要删除那个6

Schema::create('events', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->string('title', 255);
        $table->integer('join_id')->unique();
        $table->string('image')->nullable();
        $table->string('location')->nullable();
        $table->text('description')->nullable();
        $table->timestamp('event_date')->nullable();
        $table->timestamp('event_time')->nullable();
        $table->foreignUuid('user_id')->nullable()->index();
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
        $table->softDeletes();
        $table->timestamps();
    });
© www.soinside.com 2019 - 2024. All rights reserved.