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

时间:2017-12-09 14:41:30

标签: php mysql laravel

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

  

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

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在我的情况下,Unknown "sylius_calculate_original_price" filter. Did you mean "sylius_calculate_price"? 之后导致php migrate:rollback并没有解决。因为数据库中的Nothing to rollback表为空。 因此,解决方案是打开作曲家的修补程序

migrations

这是上述命令执行的结果

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

在Connection.php第647行中:                                                                                     SQLSTATE [42S01]:基本表或视图已存在:1050表“用户”   已经存在(SQL:创建表nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate users int unsigned not   auto_increment主键为空,id varchar(255)不为空,   name varchar(255)不为null,email varchar(255)不为null,   password varchar(100)为空,remember_token时间戳为空,   created_at时间戳为null)默认字符集utf8mb4   整理utf8mb4_unicode_ci)

     

在Connection.php第449行中:                                                                                     SQLSTATE [42S01]:基本表或视图已存在:1050表“用户”   阿迪的存在

updated_at

还定义方法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$ (如果不存在)。
否则,它将显示

  

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

ABC

答案 2 :(得分:0)

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

    });
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这是我的情况:

Illuminate \ Database \ QueryException:SQLSTATE [42S01]:基表或 视图已经存在:1050表“迁移”已经存在(SQL: 创建表migrationsid bigint unsigned not null auto_increment主键,created_at时间戳为空,updated_at 时间戳记null)默认字符集utf8mb4整理 'utf8mb4_unicode_ci')

解决方案:

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

答案 5 :(得分:-1)

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

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

相关问题