簡體   English   中英

Laravel不允許我遷移表,因為它已經存在

[英]Laravel won't let me migrate a table because it already exists

我正在嘗試使用Laravel Migration來創建SQL表,但它不會讓我。

這是錯誤:

SQLSTATE [42S01]:基表或視圖已存在:1050表'mytable'已存在

這是我的代碼:

        Schema::create('mytable', function (Blueprint $table) {
            $table->increments('id');
            $table->foreign('othertable_id')
                ->references('id')->on('othertable')
                ->onDelete('cascade');
            $table->string('variable');
            $table->timestamps();
    });

    }

    {
        Schema::drop('mytable');
    }

我還檢查了其他遷移是否被稱為“mytable”,但它們不是,所以我不確定它來自何處。

我嘗試了以下方法:

第一

php artisan migrate:refresh

這給了我一個錯誤

然后我刪除了整個數據庫altogheter並使用:

php artisan migrate

仍然有錯誤

在laravel 5.5和>你可以使用:

  $ php artisan migrate:fresh 
  // ( not migrate:refresh wich is a different command)

此命令首先刪除所有表,然后重新運行所有遷移

我既沒有刪除遷移條目也沒有從數據庫中手動刪除表在我的情況下,解決方案打開了作曲家的修補程序

$ php artisan tinker
>>> Schema::drop('users')
>>> Schema::drop('password_resets')
>>> Schema::drop('orders')
>>> exit
php artisan migrate

這是執行上述命令的結果

nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate

在Connection.php第647行中:SQLSTATE [42S01]:基表或視圖已存在:1050表'users'arn ady存在(SQL:create table usersid int unsigned not null auto_incr ement primary key, name varchar(255)not null, email varchar(255)not n ull, password varchar(255)not null, remember_token varchar(100)null, created_at timestamp null, updated_at timestamp null)默認字符集utf8mb4 collat​​e utf8mb4_unicode_ci)

在Connection.php第449行:SQLSTATE [42S01]:基表或視圖已存在:1050表'用戶'已存在

nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate:rollback
Nothing to rollback.
nishanth@localhost:~/Desktop/html/hutch$ php artisan tinker
Psy Shell v0.8.17 (PHP 7.1.20-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
>>> Schema::drop('password_resets')
=> null
>>> Schema::drop('orders')
=> null
>>> exit
Exit:  Goodbye.
nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2018_08_18_071213_create_orders_table
Migrated:  2018_08_18_071213_create_orders_table
nishanth@localhost:~/Desktop/html/hutch$ 

如果方法不存在,還要定義方法down()
否則,它會顯示

SQLSTATE [42S02]:找不到基表或視圖:1051未知表'XYZ.ABC'(SQL:drop table ABC

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

嘗試這個,

添加以下行:
Schema::dropIfExists('mytable'); 在為mytable創建模式之前,正好在開始的Up()函數內部。
即在遵循代碼之前。
Schema::create('mytable', function (Blueprint $table)

該錯誤表示表mytable已經存在於DB中。 您應該回滾遷移:

php artisan migrate:rollback

並再次遷移:

php artisan migrate

執行composer dump ,從數據庫中手動刪除表,並刪除要從遷移表中刪除的表的遷移條目,然后再次重新運行遷移以查看發生的情況。

如果您在DB中有表並且不希望遷移創建它,則可以在創建之前通過檢查Schema :: hasTable來忽略它

public function up()
{
    if(Schema::hasTable('products')) return;       //add this line to migration file
    Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
});

}

轉到app> Providers> AppServiceProvider.php並將其粘貼到頂部:

use Illuminate\Support\Facades\Schema;

然后轉到它里面的函數名為'Boot()'並復制粘貼這個:

Schema::defaultStringLength(191);

現在轉到您的數據庫並完全刪除您的數據庫,然后轉到控制台:

php artisan cache:clear
php artisan config:cache

然后創建一個具有相同名稱的新數據庫並返回到consol並寫入:

 php artisan migrate (congrats your database now shows em all)

暫無
暫無

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

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