Laravel-mongodb: Unable to drop MongoDB collection with migrate:fresh

Created on 30 Oct 2018  路  5Comments  路  Source: jenssegers/laravel-mongodb

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Jenssegers\Mongodb\Schema\Blueprint;

class CreateDriversTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('mongodb')->create('drivers', function (Blueprint $collection) {
            $collection->string('first_name');
            $collection->string('last_name');
            $collection->string('id_number');
            $collection->string('username');
            $collection->string('email')->nullable();
            $collection->string('password');
            $collection->string('api_token');
            $collection->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('mongodb')->drop('drivers');
    }
}

When I run migrate:fresh with my migration organised as the above, the error log says

a collection 'example.drivers' already exists

When I move Schema::connection('mongodb')->drop('drivers') into the up() function of the migration file, the collection is dropped and all works correctly. The down function is being reached, but it is almost as if the statement inside it is being ignored.

question

All 5 comments

Mongodb does not support schema's like you are defining them.
Check the documentation for supported operations:
image

your drop statement should work though

Thanks for the response.
Unfortunately even following the documentation, the collection does not drop

dropIfExists has never failed me so far. Also do not specify cast Blueprints, omit it.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

class CreateEventAuditCollection extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
            Schema::connection('audit')->create('event_audits', function ($collection) {
            $collection->index('user_id');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('audit')->dropIfExists('event_audits');
    }
}

`

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

class CreateServicesMongoTable extends Migration
{

protected $connection = 'mongodb'; /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::connection($this->connection)->create('collections', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->boolean('withUsers')->default('false');
        $table->string('type');
        $table->timestamps();
    });

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::connection($this->connection)->drop('services');
}

}
`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naveedyasin picture naveedyasin  路  3Comments

phuocduy1988 picture phuocduy1988  路  3Comments

BlakeGardner picture BlakeGardner  路  3Comments

viacheslavpleshkov picture viacheslavpleshkov  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments