【问题标题】:how to convert bigIncrements('id') to uuid('id') as the primary key?如何将 bigIncrements('id') 转换为 uuid('id') 作为主键?
【发布时间】:2018-07-19 04:23:48
【问题描述】:

目前,我们的系统将 BIINTS 作为主键,但现在我们正在转向 GUIDS/UUIDS,我们需要转换我们的表。我认为在新迁移中这样的事情可能会起作用,但不幸的是,它抱怨 ID 列已经存在。

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropPrimary('PRIMARY');
        $table->dropColumn('id');

        $table->uuid('id');
        $table->primary('id');
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->primary('id');
    });
}

在 Laravel 迁移中是否有适当的方法来执行此切换?

感谢您的帮助! :)

【问题讨论】:

    标签: mysql laravel migration uuid


    【解决方案1】:

    PRIMARY更改为users_id_primary并分离操作:

    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropPrimary('users_id_primary');
            $table->dropColumn('id');
        });
    
        Schema::table('users', function (Blueprint $table) {
            $table->uuid('id');
            $table->primary('id');
        });
    }
    

    【讨论】:

    • 我收到以下错误[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'PRIMARY'; check that column/key exists (SQL: alter table users` drop primary key)` [Doctrine\DBAL\Driver\PDOException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'PRIMARY'; check that column/key exists [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'PRIMARY'; check that column/key exists
    • @SegFaultDev 那是因为你没有使用我的代码,但是你使用了你的。
    • 我使用的是您提供的代码。我会再试一次以确认。
    • @SegFaultDev 不,您使用了您的代码,因为您显示的错误是 PRIMARY 而不是 users_id_primary
    • 嘿阿列克谢。我很抱歉。该特定错误并非来自您发布的代码。这是错误---[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it mus t be defined as a key (SQL: alter table users`删除主键)`[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it mus t be defined as a key
    猜你喜欢
    • 2019-08-06
    • 2011-09-26
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2018-06-06
    • 1970-01-01
    相关资源
    最近更新 更多