簡體   English   中英

我想運行 php artisan migrate 命令,但出現錯誤

[英]I want to run the php artisan migrate command but I get an error

我有一個項目,我想遷移數據庫文件,但是當我嘗試運行 PHP artisan migrate 命令時出現錯誤

我搜索了很多,但我沒有得到任何有用的結果,請幫助我

這是我的數據庫文件代碼:

<?php

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

class CreateBlogTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id');
            $table->string('name');
            $table->string('text');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('blogs');
    }

}

這是一條錯誤消息

Migrating: 2022_07_12_122514_create_blog_table

   Illuminate\Database\QueryException

  SQLSTATE[HY000]: General error: 1 table "blogs" already exists (SQL: create table "blogs" ("id" integer
not null primary key autoincrement, "user_id" integer not null, "name" varchar not null, "text" varchar not null, "created_at" datetime, "updated_at" datetime))

  at C:\Users\Modiran\Desktop\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
    708▕         // If an exception occurs when attempting to run a query, we'll format the error
    709▕         // message to include the bindings with SQL, which will make this exception a
    710▕         // lot more helpful to the developer instead of just the database's errors.
    711▕         catch (Exception $e) {
  ➜ 712▕             throw new QueryException(
    713▕                 $query, $this->prepareBindings($bindings), $e
    714▕             );
    715▕         }
    716▕     }

    716▕     }


  1   C:\Users\Modiran\Desktop\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:495
      PDOException::("SQLSTATE[HY000]: General error: 1 table "blogs" already exists")

  2   C:\Users\Modiran\Desktop\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:495
      PDO::prepare("create table "blogs" ("id" integer not null primary key autoincrement, "user_id" integer not null, "name" varchar not null, "text" varchar not null, "created_at" datetime, "updated_at" datetime)")

與錯誤狀態一樣,該表已經存在。

當您第一次運行遷移時,在創建表后遇到錯誤時,可能會發生這種情況。

解決此問題的最簡單方法是登錄數據庫並手動刪除表,然后再次運行遷移。

您可以運行“php artisan migrate:fresh”現有表將被自動刪除並重新生成表

我認為您的錯誤的解決方案是“學習 laravel”。

查看以下網址: 關於 stackoverflow 的官方文檔問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM