You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:55:12