![](/img/trans.png)
[英]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.