簡體   English   中英

SQLSTATE[42S01]:基表或視圖已存在:1050 表“付款”已存在(SQL:創建表“付款”

[英]SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'payments' already exists (SQL: create table `payments`

當我遷移一個表時,我看到這個錯誤,

SQLSTATE[42S01]:基表或視圖已存在:1050 表“付款”已存在(SQL:創建表payments

<?php

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

class CreatePaymentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('resnumber');
            $table->string('course_id')->default('vip');
            $table->string('price');
            $table->boolean('payment')->default(false);
            $table->timestamps();
        });
    }

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

錯誤

如果您使用的是 Laravel 5.5,則可以執行php artisan migrate:fresh 此命令將刪除所有表,然后再次創建它們。 我希望它有幫助。

如果您在數據庫中有表並且不想遷移創建它,您可以通過在創建之前檢查 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();

    });
}

如果要重新創建表,請先運行php migrate:rollback以刪除現有表。 此命令將在遷移中運行down()方法。

然后運行php migrate再次創建表。

在我的情況下php migrate:rollback導致Nothing to rollback並且沒有解決。 因為數據庫中的migrations表是空的。 所以解決方案是打開作曲家的修補程序

$ 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]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users ( id 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]: Base table or view already exists: 1050 Table 'users' alre ady exists

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:刪除表ABC

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

首先使 php artisan migrate:rollback; 然后轉到php我的管理面板並刪除剩余的表。 然后做 php artisan migrate:fresh 它會起作用:-)

這是我的場景:

Illuminate\\Database\\QueryException : SQLSTATE[42S01]: 基表或視圖已經存在:1050 表“遷移”已經存在(SQL:創建表migrationsid bigint unsigned not null auto_increment 主鍵, created_at timestamp null, updated_at timestamp null)默認字符集 utf8mb4 整理 'utf8mb4_unicode_ci')

解決方案:

  1. 刪除位於YOUR-PROJECT/database/migrations/2020_02_04_031638_create_migrations_table.php 中的文件
  2. 運行php artisan migrate ,然后您的新表將在您的數據庫中創建

如果您使用的是 laravel 9 版本,請使用此命令

php 工匠遷移:新鮮

該命令將對您有所幫助。 這對我有用。

根據報錯,數據庫表已經存在。 以下是可能有用的可能解決方案。

解決方案 1:手動刪除整個數據庫表,包括遷移表,然后重新運行php artisan migrate命令。

解決方案 2:如果您在某些現有表中有一些記錄並且您不想刪除/刪除它們。 然后,單擊您的 phpMyAdmin 或相關人員中的migrations表以查看已遷移表的列表。 刪除/編輯導致錯誤的特定表,然后再次運行php artisan migrate命令。

解決方案 3:出於某種原因,您可能想要運行特定的遷移文件,下面的命令就是您所需要的。

php artisan migrate:refresh --path=/database/migrations/file-name.php

請記住將file-name.php替換為您的實際文件名。

我希望這對某人有幫助。 :)

暫無
暫無

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

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