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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47729713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:04:22  来源:igfitidea点击:

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

phpmysqllaravel

提问by

When I migrate a table I see this error,

当我迁移表时,我看到此错误,

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

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

Error

错误

采纳答案by Alexey Mezenin

If you want to recreate the table, run php migrate:rollbackfirst to delete the existing one. This command will run the down()method in the migration.

如果要重新创建表,php migrate:rollback请先运行以删除现有表。此命令将运行down()迁移中的方法。

Then run php migrateto create the table again.

然后php migrate再次运行以创建表。

回答by larsbadke

If you are on Laravel 5.5 you can do php artisan migrate:fresh. This command will drop all tables und then create them again. I hope it helps.

如果您使用的是 Laravel 5.5,则可以执行php artisan migrate:fresh. 此命令将删除所有表,然后再次创建它们。我希望它有帮助。

回答by Mohsen

If you have table in DB and dont want migrate create it, you can ignore it by check Schema::hasTable before create

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

    });
}

回答by prasanna purohit

first make php artisan migrate:rollback; then go to php my admin panel and drop the remaining table. then do php artisan migrate:fresh it will work :-)

首先使 php artisan migrate:rollback; 然后转到php我的管理面板并删除剩余的表。然后做 php artisan migrate:fresh 它会起作用:-)

回答by Aqua Huang

This is my scenario:

这是我的场景:

Illuminate\Database\QueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'migrations' already exists (SQL: create table migrations(idbigint unsigned not null auto_increment primary key, created_attimestamp null, updated_attimestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

Illuminate\Database\QueryException:SQLSTATE[42S01]:基表或视图已经存在:1050 表'迁移'已经存在(SQL:创建表migrationsidbigint unsigned not null auto_increment 主键,created_attimestamp null,updated_attimestamp null)默认字符集utf8mb4 collat​​e 'utf8mb4_unicode_ci')

solution :

解决方案 :

  1. delete the file located in YOUR-PROJECT/database/migrations/2020_02_04_031638_create_migrations_table.php
  2. run php artisan migrate, then your new table will create in your database
  1. 删除位于YOUR-PROJECT/database/migrations/2020_02_04_031638_create_migrations_table.php 中的文件
  2. 运行php artisan migrate,然后您的新表将在您的数据库中创建

回答by N?s????? ?

In my case after php migrate:rollbackresulted in Nothing to rollbackand didn't solve. Because the migrationstable in the database is empty. So the solution is open up the tinker from the composer

在我的情况下,php migrate:rollback导致Nothing to rollback并没有解决。因为migrations数据库中的表是空的。所以解决方案是打开作曲家的修补程序

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

Here is the result of the above commands executed

下面是上面命令的执行结果

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

In Connection.php line 647: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users(idint unsigned not null auto_incr ement primary key, namevarchar(255) not null, emailvarchar(255) not n ull, passwordvarchar(255) not null, remember_tokenvarchar(100) null, created_attimestamp null, updated_attimestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

In Connection.php line 449: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre ady exists

在 Connection.php 第 647 行:SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users( idint unsigned not null auto_incr ement primary key, namevarchar(255) not null, emailvarchar( 255) not n ull, passwordvarchar(255) not null, remember_tokenvarchar(100) null, created_attimestamp null, updated_attimestamp 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$ 

Also define the method down(), if it doesn't exist.
Otherwise, it'll show

还要定义方法down(),如果它不存在。
否则会显示

SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'XYZ.ABC' (SQL: drop table ABC)

SQLSTATE[42S02]:未找到基表或视图:1051 未知表 'XYZ.ABC'(SQL:删除表ABC

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