」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何解決 Laravel 5.5 遷移中的「基底表已存在」錯誤?

如何解決 Laravel 5.5 遷移中的「基底表已存在」錯誤?

發佈於2024-11-08
瀏覽:988

How to Resolve the \

Laravel 5.5 錯誤處理:解決遷移的「基底表已存在」

遇到錯誤「基底表或視圖已存在」(在Laravel 5.5 中執行php artisan migrate 指令時出現錯誤代碼1050)可能會令人沮喪。此錯誤表示遷移中指定的資料庫表已存在。

故障排除與解決方法

  1. 檢視指令: Double-檢查您正在執行的指令。確保您引用正確的遷移檔案。
  2. 檢查表存在性: 手動檢查相關表(例如,所提供範例中的使用者)是否已存在於資料庫中。您可以使用 MySQL Workbench 或 phpMyAdmin 等資料庫管理工具來驗證這一點。
  3. 刪除現有表: 如果表已經存在,您可以使用以下命令刪除它: php artisan migrate:rollback --step=1,其中--step=1 表示您想要回滾第一次(也是唯一一次)遷移。
  4. 修改遷移檔案:檢查 create_users_table。解決方案中提供的 php 遷移檔案。它確保用戶表在重新建立之前被刪除。
  5. 再次運行遷移:修改遷移檔案或刪除現有表後,嘗試執行php artisan migrate 指令

範例遷移檔案

以下create_users_table.php 遷移的修改版本應該可以解決該問題:

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
版本聲明 本文轉載於:1729667489如有侵犯,請洽[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3