Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL view will not drop in Laravel 5.5 migration

I am trying to write a PHPUnit test case in Laravel 5.5. I'd like to run migrations as part of that process. My users migration script actually creates a view, not a table. And it seems that all other normal tables get created and dropped just as expected. But the users view does not. Any ideas why? I can manually run the DROP statement just fine in PHPMyAdmin. Here is my migration script:

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

class CreateUsersTable extends Migration
{

public function up()
{
    DB::statement("
    CREATE VIEW users AS
    (
      SELECT
        PD.ID AS id,
        PCI.Username AS username,
        PCI.Username AS first_name
      FROM table1.Person PD
      LEFT JOIN table2.Contact PCI ON PCI.ID = PD.ID
    )
");

}

  public function down()
  {
    DB::statement("DROP VIEW users");
  }
}
like image 374
Amy Lashley Avatar asked Oct 27 '25 01:10

Amy Lashley


1 Answers

Yes, this is an open issue in the Laravel github. The proposed workaround which worked for me is to replace the CREATE VIEW in my migration file with CREATE OR REPLACE. So the full up() function will be:

public function up()
{
DB::statement("
CREATE OR REPLACE VIEW users AS
(
  SELECT
    PD.ID AS id,
    PCI.Username AS username,
    PCI.Username AS first_name
  FROM table1.Person PD
  LEFT JOIN table2.Contact PCI ON PCI.ID = PD.ID
)
");

}
like image 65
Amy Lashley Avatar answered Oct 29 '25 18:10

Amy Lashley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!