微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为 postgresql 数组数据类型创建 Laravel 迁移

如何解决如何为 postgresql 数组数据类型创建 Laravel 迁移

我有以下laravel迁移up方法

public function up()
{
    Schema::create('users',function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('mobile')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        // $table->string('sports');
        $table->date('dob');
        $table->string('height');
        $table->rememberToken();
        $table->timestamps();
    });

    DB::statement('ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER password');
}

当我运行迁移时,它会显示 sqlSTATE[42601]: Syntax error: 7 ERROR: Syntax error at or near "text" 第 1 行:ALTER TABLE 用户 ADD COLUMN sports TYPE text[] AFTER passwo... ^(sql:ALTER TABLE 用户 ADD COLUMN sports TYPE text[] AFTER password)。我不知道那里有什么问题?

解决方法

请尝试像这样的 Sql 语句:

 DB::statement('alter table users alter sports drop default');
    DB::statement('alter table users alter sports type text[] using array[sports]');
    DB::statement("alter table users alter sports set default '{}'");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。