Bram Klapwijk
Bram Klapwijk

Reputation: 11

SQLite Expecting a string as Index Column when dropping a column

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class RemoveConstructionDateToOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasColumn('orders', 'construction_date')) {
            Schema::table('orders', function (Blueprint $table) {
                $table->dropColumn('construction_date');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->date('construction_date');
        });
    }
}

When I am migrating this laravel migration on a sqlite database, I am getting the following error.

InvalidArgumentException: Expecting a string as Index Column

Upvotes: 1

Views: 1522

Answers (2)

jpswade
jpswade

Reputation: 355

SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification.

Look back through your migration files and separate out the dropColumn / renameColumn into their own modification blocks.

Upvotes: 1

Hop hop
Hop hop

Reputation: 856

I found where the problem is after hours of poking around. Looks like the doctrine/dbal package cannot rename/handle/drop?! SQLite columns that already have an index

So my solution was to NOT make any indexes on columns that get later renamed in the migrations

Of course we do want the indexes to exist in MySQL so here's my solution to it.

     Schema::table('comments', function (Blueprint $table) {
        $table->string('commentable_type', 100)->nullable()->after('id');

        //only add index if the connection is mysql
        if(config('DB_CONNECTION') === 'mysql'){
            $table->index('commentable_type');
        }
    });

So later when you try to rename the column and using SQLite it works.

Upvotes: 1

Related Questions