Laravel 10通过迁移修改MySQL引擎(InnoDB转MyISAM)失败求助
解决Laravel迁移修改MySQL表引擎无效的问题
问题原因
你当前的修改迁移代码无法生效,是因为在Schema::table()的闭包中设置$table->engine不会触发引擎变更操作——Laravel的Schema::table()方法仅用于处理表结构的常规变更(如添加字段、索引等),不会为这种引擎设置生成对应的ALTER TABLE语句。
正确解决方案
情况1:尚未运行创建products表的迁移
直接在创建表的迁移中指定引擎:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('products', function (Blueprint $table) { $table->engine = 'MyISAM'; // 在这里指定引擎 $table->id(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('products'); } };
情况2:已运行创建表的迁移,需要修改现有表引擎
修改你的alter_products_engine迁移,使用原生SQL语句执行引擎变更:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\DB; return new class extends Migration { public function up(): void { DB::statement('ALTER TABLE products ENGINE = MyISAM'); } public function down(): void { DB::statement('ALTER TABLE products ENGINE = InnoDB'); } };
注意:记得导入
DB门面(use Illuminate\Support\Facades\DB;),否则会报错。
验证结果
执行迁移后,可通过MySQL命令验证引擎是否变更:
SHOW TABLE STATUS LIKE 'products';
查看结果中的Engine字段,确认是否为MyISAM。
内容的提问来源于stack exchange,提问作者Mina Shaker
相关产品推荐
相关产品推荐

