簡體   English   中英

我為什么不能在Laravel遷移

[英]How come I can't migrate in laravel

laravel中的遷移文件用於在數據庫中創建表,對嗎? 但是,每當我嘗試遷移時,都會出現此錯誤:

C:\\ xampp \\ htdocs \\ app> php artisan遷移

[Illuminate \\ Database \\ QueryException] SQLSTATE [42S01]:基本表或視圖已存在:1050表'users'已存在(SQL:創建表usersid int unsigned不為null auto_increment主鍵, name varchar(255)不為null, email VARCHAR(255)NOT NULL, password為varchar(255)NOT NULL, remember_token為varchar(100)空, created_at時間戳空, updated_at添estamp空)默認字符集utf8mb4整理utf8mb4_unicode_ci)

[PDOException] SQLSTATE [42S01]:基表或視圖已存在:1050表“用戶”已存在


我創建了一個新的遷移文件,稱為測試。 我知道用戶已經存在,但是我想創建我創建的新表,稱為test。

這是我將用來創建表但不會創建的遷移文件:

    <?php

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

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

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

這是告訴我它存在的用戶遷移文件:

<?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::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');
    }
}

這是密碼遷移文件:

<?php

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

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

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

這是我沒有談論的虛擬遷移文件,因為我認為這不是問題:

<?php

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

class CreateDummiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('dummies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('body');
            $table->timestamp('date'); //if you dont put name for the timestamp it will create: create_at and update_at fields.
        });
    }

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

很抱歉讓你們久等了,我花了很長時間才找到編輯按鈕並修復代碼間距。 這是我第一次使用堆棧溢出。

數據庫中的migrations表與數據庫的其余部分不同步。 Laravel試圖重新運行遷移以創建users表,但失敗了。

如果這是一個全新項目,則可以從數據庫中刪除所有表,然后重新運行所有遷移。 或者,修復您的遷移表,以使其准確反映數據庫的狀態。

在您的laravel / app / providers / AppServiceProvider.php中,在啟動內添加以下代碼

Schema::defaultStringLength(191);

然后嘗試php artisan migrate:refresh

或最好

刪除所有表並再次運行php artisan migrate

在所有表的up function()開頭添加以下代碼

public function up()
{
        // Add this line
        Schema::dropIfExists('users');
        // from you down method
        Schema::create('users', function (Blueprint $table) {
            ...........
        });
}

嘗試這個...

*// AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

function boot()
{
    Schema::defaultStringLength(191);
}
*

暫無
暫無

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

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