0

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?

2 Answers 2

1

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 '{}'");
Sign up to request clarification or add additional context in comments.

9 Comments

it is working but it will add column at last, i need it after 'password' column only
what about: DB::statement('ALTER TABLE users ALTER COLUMN sports TEXT[]');
or DB::statement('ALTER TABLE users MODIFY COLUMN sports TEXT[]');
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "MODIFY" LINE 1: ALTER TABLE users MODIFY COLUMN sports TEXT[] ^ (SQL: ALTER TABLE users MODIFY COLUMN sports TEXT[])
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "TEXT" LINE 1: ALTER TABLE users ALTER COLUMN sports TEXT[] ^ (SQL: ALTER TABLE users ALTER COLUMN sports TEXT[])
|
0
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        // ...
        $table->string('sports');
        // ...
        $table->timestamps();
    });

    DB::statement('ALTER TABLE users ALTER COLUMN sports TYPE text[] USING ARRAY[sports]');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.