SQLSTATE[42S01]: 基本表或视图已存在: 1050 表 'payments' 已经存在 (SQL: 创建表 `payments`)

5

当我迁移一个表格时,我看到了这个错误:

SQLSTATE[42S01]: 基础表或视图已经存在:1050 表 'payments' 已经存在 (SQL: create table 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');
    }
}

Error


你已经创建了 payments 表吗?同时能否分享所有表的截图? - Niklesh Raut
这可能是由于数据库中仍存在某些表格,因此您可以删除它们来解决问题。 - Elxon
8个回答

8
如果您使用的是 Laravel 5.5,可以执行 php artisan migrate:fresh 命令。该命令将删除所有表格并重新创建它们。希望这有所帮助。

2
如果您在数据库中有表格,但不想迁移创建它,您可以在创建之前通过检查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();

    });
}

1
如果您想重新创建表格,请先运行php migrate:rollback以删除现有的表格。此命令将运行迁移中的down()方法。
然后运行php migrate以再次创建表格。

0

这是我的情况:

Illuminate\Database\QueryException : SQLSTATE[42S01]: 基本表或视图已经存在:1050 表 'migrations' 已经存在 (SQL: create table migrations (id bigint unsigned not null auto_increment primary key, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

解决方案:

  1. 删除位于YOUR-PROJECT/database/migrations/2020_02_04_031638_create_migrations_table.php的文件
  2. 运行php artisan migrate,然后您的新表将在数据库中创建

0

首先执行 php artisan migrate:rollback; 然后进入 php my admin 面板并删除剩余的表格。然后执行 php artisan migrate:fresh,就可以正常工作了 :-)


0

如果您正在使用 Laravel 9 版本,请使用此命令

php artisan migrate:fresh

这个命令会对您有所帮助,对我来说很有效。


0

根据错误提示,数据库表已经存在。以下是可能有帮助的解决方案。

解决方案1: 手动删除整个数据库表,包括迁移表,然后重新运行php artisan migrate命令。

解决方案2: 如果您在某些现有表中有一些记录,并且不想删除它们。那么,请从您的phpMyAdmin或相关的地方单击migrations表以查看已迁移表的列表。删除/编辑导致错误的特定表,然后再次运行php artisan migrate命令。

解决方案3: 出于某种原因,您可能想要运行特定的迁移文件,下面的命令就是您需要的。

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

记得用实际的文件名替换file-name.php

希望这对某人有所帮助。 :)


0
在我的情况下,执行php migrate:rollback命令后出现了Nothing to rollback的提示,但并没有解决问题。这是因为数据库中的migrations表为空。 因此,解决方法是从composer中打开tinker。
$ 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' 已经存在 (SQL: create table users (id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
在 Connection.php 的 449 行:
SQLSTATE[42S01]: 基础表或视图已经存在: 1050 表 'users' 已经存在
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');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接