[英]SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'payments' already exists (SQL: create table `payments`
[英]SQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Table
這是我的遷移表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->string('name');
$table->string('email',50)->unique();
$table->string('username');
$table->string('password');
$table->boolean('active');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
每當我嘗試遷移時,都會發生以下錯誤
* [Illuminate \\ Database \\ QueryException] SQLSTATE [42S01]:基本表或視圖已存在:1050表'users'確實存在(SQL:創建表users
( id
int無符號不為空auto_increment主鍵, role_id
int無符號不為空, name
為varchar(191)不為空, email
VARCHAR(50)不為空, username
VARCHAR(191)不為空, pas sword
VARCHAR(191)不為空, active
TINYINT(1)不為空, remember_token
VARCHAR(100)無效, created_at
時間戳空, updated_at
時間戳NU LL)默認字符集utf8mb4整理utf8mb4_unicode_ci)
[PDOException] SQLSTATE [42S01]:基本表或視圖已存在:1050表'用戶'確實存在*
請告訴我該怎么辦? 我已經使用過migration:reset或dumpautoload或rollback沒有任何反應。 很多時間,我編輯或刪除了這個usersfile並重新創建了它。
這樣嘗試
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserTable extends Migration
{
public function up()
{
Schema::dropIfExists('users');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->string('name');
$table->string('email',50)->unique();
$table->string('username');
$table->string('password');
$table->boolean('active');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
當您要遷移數據庫中存在的表時,將顯示此問題:對於此問題,只需手動轉到PHPMYADMIN並刪除所有表,然后運行命令php artisan migration
危險 -通過刪除表,回滾或刷新數據庫將破壞該表或數據庫中的所有數據,並且不建議將這些方法用於生產環境,並且絕對不應在遷移中包括刪除表的行為。
顯然,有很多針對此問題的“解決方案”,但是我閱讀的所有解決方案都是非常具有破壞性的解決方案,並且都不適用於生產數據庫。 是的,他們擺脫了錯誤,但是付出了什么代價呢?
根據解決方案的數量,似乎也可能有多種原因導致此錯誤。
我的錯誤是由我的遷移表中的條目缺失引起的。 這需要一個非常簡單的解決方案-重新添加條目。像這樣的簡單問題絕對不能保證數據庫刷新
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.