Php Laravel 5.5错误基表或视图已存在:1050表';用户';已经存在
规格:Php Laravel 5.5错误基表或视图已存在:1050表';用户';已经存在,php,mysql,laravel-5,laravel-5.5,Php,Mysql,Laravel 5,Laravel 5.5,规格: C:/Users/user/code/blog/>php artisan migrate [Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null aut o_increment primary
C:/Users/user/code/blog/>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null aut
o_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 engine = InnoDB R
OW_FORMAT=DYNAMIC)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
C:/Users/user/code/blog/>laravel new website
C:/Users/user/code/blog/>php artisan make:migration create_lists_table --create=lists
C:/Users/user/code/blog/>php artisan migrate
- Laravel版本:5.5.3
- PHP版本:7.1
- 数据库驱动程序和版本:MariaDB 10.1.26
C:/Users/user/code/blog/>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null aut
o_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 engine = InnoDB R
OW_FORMAT=DYNAMIC)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
C:/Users/user/code/blog/>laravel new website
C:/Users/user/code/blog/>php artisan make:migration create_lists_table --create=lists
C:/Users/user/code/blog/>php artisan migrate
复制步骤:
C:/Users/user/code/blog/>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null aut
o_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 engine = InnoDB R
OW_FORMAT=DYNAMIC)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
C:/Users/user/code/blog/>laravel new website
C:/Users/user/code/blog/>php artisan make:migration create_lists_table --create=lists
C:/Users/user/code/blog/>php artisan migrate
问题
它创建用户表并给出错误,但不创建列表表我自己解决了问题
通过更改我的create_users_table.php
<?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()
{
Schema::dropIfExists('users');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
以下是我为解决同一问题所采取的步骤:
在控制台中,我编写了php artisan tinker
然后,再次在控制台中,Schema::drop('users')
最后,php artisan迁移
,一切顺利
以下是我为解决同一问题所采取的步骤:
我有不同的解决方案,我删除了迁移表,这里是我的解决方案
<?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()
{
Schema::dropIfExists('migration');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
只需先从数据库中删除这些列。并运行composer更新。最后运行php artisan migrate,它将解决您的问题。您的问题的最简单解决方案。如本链接所述,有两种可能的解决方案: 首先是回滚 php artisan迁移:回滚
第二个是删除表。解决此问题的简单方法是运行以下命令 php artisan迁移:新鲜
以下命令解决了我的问题:
1. php artisan tinker
2. Schema::drop('your_table_name')
3. php artisan migrate
解决方案:
php artisan migrate
同意尼特什的回答,但我认为这不是无缘无故每次都放弃谈判桌的最佳做法。我们还可以使用条件Schema::hasTable('users')进行简单的“if”检查。因此,我们可以将其用作:
<?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();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
使用!Schema::hasTable('users'),它将检查数据库中是否已经存在表。
若你们不想放下桌子,那个么这是个好主意 使用php artisan migrate
时通常会发生迁移错误,并且您的迁移有错误
在Laravel中,有一些方法可以扭转已经发生的事情
已执行,尤其是迁移到MySql等更强健的数据库,
比如说
一种方法是逆转所采取的步骤
php artisan migrate
就是运行
php artisan migrate: rollback
它会自动反转上次执行的迁移
您还可以委托使用step参数反转的步骤数
数字显示将反转多少步
如果数据库中存在表,可以通过添加以下代码跳过迁移表:
public function up()
{
if(Schema::hasTable('users')) return; //add this line to your database file
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
通过命令创建迁移
php artisan make:迁移创建类别表
然后在数据库/迁移中设置必要的字段和数据库,然后运行此命令
pho artisan迁移——假装
它将显示sql语句,复制类别的sql语句并将其粘贴到sql中的数据库中,只需单击“运行”,您的迁移就会在那里,无需删除用户表更改您的Mysql引擎设置
在config/database.php
中,将mysql引擎设置更改为:
“引擎”=>“innodb行格式=动态”
注意:这适用于Laravel 5.2.14+有时我会遇到与您相同的问题,在我检查之后,这是因为有时我懒得键入和复制粘贴create_user_table.php中的代码
<?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()
{
Schema::dropIfExists('users');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
我忘了更改这个代码中的表名
Schema::create('users',
对此
Schema::create('what_ever_you_want_to_name_it',
我希望这会有所帮助您可以使用此命令重置所有内容
php artisan migrate:reset
然后你可以运行这个命令
php artisan migrate
请记住,重置将删除用户表。请不要使用手动方法删除表或对数据库执行任何操作。Laravel允许我们使用命令做任何事情,这就是它的美妙之处。如果您想了解有关使用Laravel迁移的详细信息,请查看此链接
清除数据库并重试again@apokryfos我已经删除了我所有的表和数据库,但它给出了相同的错误。你能把它作为你问题的答案吗?如果没有重复的条目,它可能会帮助将来遇到类似问题的其他人检查迁移文件。也许您复制了用户模式,但忘记了编辑它。如果是这样的话,Laravel会看到你试图进入“用户”模式两次。这对我来说很有效:@Florin它还会创建表。注意,这会从头开始创建所有内容,你会丢失数据。那时我知道的不多。那时我还是个13岁的孩子,就开始编程了。尝试回答问题时,我被禁止回答。它也有效,但laravel有一个更简单的形式来解决问题