Berkay
Berkay

Reputation: 103

How to drop column with laravel migration on sqlite?

I want to write test for my application, but i getting error when run the migration;

There is no column with name

My Migration file

Schema::table('bill_payments', function (Blueprint $table) {
    $table->dropColumn('attachment');
    // $table->getColumns(); return empty array
    // $table->dropColumn(['attachment']); I tried this
});

dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false

// and this is not working because return false.

if(Schema::hasColumn('bill_payments', 'attachment'))
{
    Schema::table('bill_payments', function (Blueprint $table) {
        $table->dropColumn('attachment');
    });
}

Also i add doctrine/dbal 2.5.13

i running tests using mysql, but slowly.

[Solved]

Wow! i using prefix for tables. i deleted this and now it's work.

Upvotes: 3

Views: 6974

Answers (1)

abr
abr

Reputation: 2129

One thing you need to know about migrations is that they run untill they crash or succeed, following your example:

Schema::table('bill_payments', function (Blueprint $table) {
    $table->dropColumn('attachment');
});
//It will drop the column and stop here. When you run the migration again, it will output your error because the column no longer exists.
dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false

What you should have in your migration code is having the reverse operations in the Down() method. Meaning you run the migration, it applies the Up() and when you rollback, it reverts correctly. That error is really what it means, it means that when it reaches an operation relating table bill_payments and column attachment, it recognizes that attachment doesn't exist.

Edit:

There is something related to SQlite in the documentation:

"Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported."

Upvotes: 4

Related Questions