【问题标题】:how to create laravel migration for postgresql array data type如何为 postgresql 数组数据类型创建 laravel 迁移
【发布时间】:2021-06-19 02:42:28
【问题描述】:

我有以下 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" LINE 1: ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER passwo ... ^ (SQL: ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER password)。不知道有什么问题?

【问题讨论】:

    标签: laravel postgresql laravel-migrations


    【解决方案1】:

    请尝试这样的 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 '{}'");
    

    【讨论】:

    • 它正在工作,但它最后会添加列,我只需要在“密码”列之后
    • 怎么样:DB::statement('ALTER TABLE users ALTER COLUMN sports TEXT[]');
    • 或 DB::statement('ALTER TABLE users MODIFY COLUMN sports TEXT[]');
    • SQLSTATE[42601]:语法错误:7 错误:“修改”第 1 行或附近的语法错误:ALTER TABLE users MODIFY COLUMN sports TEXT[] ^ (SQL: ALTER TABLE users MODIFY COLUMN sports TEXT [])
    • SQLSTATE[42601]:语法错误:7 错误:“TEXT”或附近的语法错误第 1 行:ALTER TABLE users ALTER COLUMN sports TEXT[] ^(SQL:ALTER TABLE users ALTER COLUMN sports TEXT [])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2019-01-09
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多