Search code examples
laravelpostgresqllaravel-migrations

how to create laravel migration for postgresql array data type


I have the following laravel migration up method

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');
}

when I run migration it will show 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). I don't know what's a problem there?


Solution

  • please try your Sql statement like this:

     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 '{}'");