Laravel 9迁移报错SQLSTATE[42000],表名不正确如何修复?
Laravel 9迁移报错SQLSTATE[42000]的修复方案
问题根源
从错误信息的SQL语句能看到,生成的字段定义出现了.`meta_title```这种异常格式——这是因为你在迁移代码里给字段名加了开头的`.`,Laravel会把这个点解析为**表名与字段名的分隔符**,于是出现了空表名()加字段名的错误写法,触发MySQL的1103 Incorrect table name报错。
修复步骤
直接修改迁移文件中的三个字段,去掉字段名开头的.:
错误字段代码:
$table->string('.meta_title'); $table->string('.meta_description'); $table->string('.meta_keyword');
修正后的字段代码:
$table->string('meta_title'); $table->string('meta_description'); $table->string('meta_keyword');
修改后的完整迁移代码
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('slug'); $table->longText('description'); $table->tinyInteger('status')->default('0'); $table->tinyInteger('popular')->default('0'); $table->string('meta_title'); $table->string('meta_description'); $table->string('meta_keyword'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } };
重新执行迁移
修改完成后,再次运行迁移命令即可:
php artisan migrate
内容的提问来源于stack exchange,提问作者Abdullah moiz
相关产品推荐
相关产品推荐

