Home > OS >  Drop views and tables
Drop views and tables

Time:12-23

I have a migration that create a view in MySQL. when I run

PHP artisan db:wipe

it drops all the tables but not the view.

when I run

php artisan migrate

it says base table or view already exists.

my migration

    public function up()
    {
        DB::statement("
            CREATE VIEW contacts_view 
            AS
            SELECT
            *
            FROM
                accounts
            where isContact='yes';
        ");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Schema::dropIfExists('contacts_view');
        DB::statement("DROP VIEW contacts_view");
    }

CodePudding user response:

To avoid errors when running your migration with existing view, sue CREATE OR REPLACE

public function up()
{
    DB::statement("
        CREATE OR REPLACE VIEW contacts_view 
        AS
        SELECT
        *
        FROM
            accounts
        where isContact='yes';
    ");
}

The view will always be recreated.

  • Related