[Laravel][mysql]DBのビュー作成時に連番を付与したい

前提

Laravelで管理システムを作っています。

DBのビューをマイグレーションファイルで作成しています。
テスト環境上のDockerでマイグレーション実行時にはエラーが出ませんでしたが、レンタルサーバのconoha上でマイグレーション実行時にエラーが出ました。

・dockerのmysqlのバージョン
mysql Ver 8.0.27 for Linux on x86_64 (MySQL Community Server - GPL)
・conohaのmysqlのバージョン
mysql Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using EditLine wrapper

マイグレーションファイルに、連番付与のため以下の関数を利用したことが原因だというところまでは特定しました。
ROW_NUMBER() OVER(ORDER BY email) id,

実現したいこと

ROW_NUMBER()以外を利用して、連番を付与したビューを作成したい

発生している問題・エラーメッセージ

conoha上で実行。
usersテーブルは既にマイグレートされています。

$ php artisan migrate Migrating: 2022_08_28_055050_create_merge_customers_table Illuminate\Database\QueryException SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY email) id, email FROM users' at line 3 (SQL: CREATE VIEW merge_customers AS SELECT ROW_NUMBER() OVER(ORDER BY email) id, email FROM users) at vendor/laravel/framework/src/Illuminate/Database/Connection.php:742 738▕ // If an exception occurs when attempting to run a query, we'll format the error 739▕ // message to include the bindings with SQL, which will make this exception a 740▕ // lot more helpful to the developer instead of just the database's errors. 741▕ catch (Exception $e) { ➜ 742▕ throw new QueryException( 743▕ $query, $this->prepareBindings($bindings), $e 744▕ ); 745▕ } 746▕ } +7 vendor frames 8 database/migrations/2022_08_28_055050_create_merge_customers_table.php:16 Illuminate\Support\Facades\Facade::__callStatic() +33 vendor frames 42 artisan:37 Illuminate\Foundation\Console\Kernel::handle()

該当のソースコード

database/migrations/2022_08_28_055050_create_merge_customers_table.php

php

<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;return new class extends Migration{ * Run the migrations. * public function up() { \DB::statement($this->createView()); } // 顧客をマージしたビューを作成 private function createView(): string { $sql = <<<EOT CREATE VIEW merge_customers AS SELECT ROW_NUMBER() OVER(ORDER BY email) id, email FROM users EOT; return $sql; } * Reverse the migrations. * public function down() { \DB::statement($this->dropView()); } private function dropView(): string { $sql = <<<EOT DROP VIEW IF EXISTS `merge_customers`; EOT; return $sql; }};

試したこと

① ROW_NUMBER()の代わりに、@numを使用
database/migrations/2022_08_28_055050_create_merge_customers_table.phpのビュー作成のSQL文を変更

php

$sql = <<<EOT CREATE VIEW merge_customers AS SELECT @num:=@num+1 AS id, email FROM users, (SELECT @num:=0) AS dummy_t EOT;

conoha上でマイグレーション実行

$ php artisan migrate:refresh --path=/database/migrations/2022_08_28_055050_create_merge_customers_table.php Migrating: 2022_08_28_055050_create_merge_customers_table Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1351 View's SELECT contains a variable or parameter (SQL: CREATE VIEW merge_customers AS SELECT @num:=@num+1 AS id, email FROM users, (SELECT @num:=0) AS dummy_t) at vendor/laravel/framework/src/Illuminate/Database/Connection.php:742 738▕ // If an exception occurs when attempting to run a query, we'll format the error 739▕ // message to include the bindings with SQL, which will make this exception a 740▕ // lot more helpful to the developer instead of just the database's errors. 741▕ catch (Exception $e) { ➜ 742▕ throw new QueryException( 743▕ $query, $this->prepareBindings($bindings), $e 744▕ ); 745▕ } 746▕ } +7 vendor frames 8 database/migrations/2022_08_28_055050_create_merge_customers_table.php:16 Illuminate\Support\Facades\Facade::__callStatic() +33 vendor frames 42 artisan:37 Illuminate\Foundation\Console\Kernel::handle()

② @num以外での方法を調査
以下のサイトを見つけたが、難しくて理解できていないので、もっと簡単な方法がないか調査中
・【MySQL】【Laravel】DBデータを取得する際に、連番を付与する方法:https://nextat.co.jp/staff/archives/138

再度実現したいこと

ROW_NUMBER()以外を利用して、連番を付与したビューをできるだけ簡単に作成したいです。
何かご助言よろしくお願いいたします。

コメントを投稿

0 コメント