【发布时间】:2019-10-07 21:27:24
【问题描述】:
当我将我的表迁移到数据库时,它给出了这个错误
SQLSTATE[42S01]:基表或视图已存在:1050 表“用户” 已经存在 (SQL: create table
users(idint unsigned not null auto_increment 主键,bodylongtext not null,urlvarchar(255) null,user_idint unsigned not null,commentable_idint unsigned not null,commentable_typevarchar(191) not null,created_a ttimestamp null,updated_attimestamp null) 默认 字符集 utf8mb4 collate utf8mb4_unicode_ci) 在 Connection.php 第 449 行: SQLSTATE[42S01]:基表或视图已存在:1050 表 “用户”已经存在
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if(!Schema::hasTable('users')){
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table-> string('name');
$table-> string('email')->unique();
$table-> string('password');
$table->rememberToken();
$table->timestamps();
});
}
Schema::table('users', function(Blueprint $table){
$table -> string('first_name') -> nullabel();
$table -> string('middle_name') -> nullabel();
$table -> string('last_name') -> nullabel();
$table -> string('city') -> nullabel();
$table -> integer('role') -> unsigned();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
我从数据库中删除了所有表然后我尝试了但给出了同样的错误
【问题讨论】:
-
试试这个代码:
php artisan migrate:fresh -
我做了但没有为我工作
-
请发布您的迁移,您是否定义了两次用户表?
-
检查您的
.env文件并查看数据库名称是否正确。
标签: php mysql laravel migration