cyrildewit / eloquent-viewable

Associate views with Eloquent models in Laravel
MIT License
825 stars 105 forks source link

Base Table or View already exists error #178

Closed simioluwatomi closed 4 years ago

simioluwatomi commented 5 years ago

Description:

I have a MySQL test database whose connection is specified in config/database.php like so

'mysql_testing' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_TESTING', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],

I can migrate this database with the database flag on the migrate command like so php artisan migrate --database=mysql_testing

After installing this library and migrating the database, I discovered that new migrations kept throwing the Base Table or View already exists error and it kept fingering the views table.

Even when I decided to do a migrate:fresh, I got the same error.

Then I went into the database migration that the library published and modified it like so

Schema::create('views', function (Blueprint $table) {
            $table->increments('id');
            $table->morphs('viewable');
            $table->text('visitor')->nullable();
            $table->string('collection')->nullable();
            $table->timestamp('viewed_at')->useCurrent();
        });

Now everything works fine.

Screenshots attached

image

image

image

So my guess is that Schema::conection defined in the migration's constructor does not respect the database flag passed into the artisan command.

public function __construct()
    {
        $this->schema = Schema::connection(
            config('eloquent-viewable.models.view.connection')
        );

        $this->table = config('eloquent-viewable.models.view.table_name');
    }
malish8y6e7 commented 5 years ago

Add -> App\Providers AppServiceProvider use Illuminate\Support\Facades\Schema;

Schema::defaultStringLength(191);