php Laravel 5.5 错误基表或视图已存在:1050 表“用户”已存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46129270/
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
Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already exists
提问by Nitesh Kumar Niranjan
Specifications:
规格:
- Laravel Version: 5.5.3
- PHP Version: 7.1
- Database Driver & Version: MariaDB 10.1.26
- Laravel 版本:5.5.3
- PHP 版本:7.1
- 数据库驱动程序和版本:MariaDB 10.1.26
Description:
描述:
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
Steps To Reproduce:
重现步骤:
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
Problem
问题
It Creates users table and give error but not creating lists table
它创建用户表并给出错误但不创建列表表
回答by Nitesh Kumar Niranjan
I Solved My Problem Myself by Changing My create_users_table.php
我通过更改我的 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');
}
}
回答by Kaloyan Drenski
Here are the steps I took to solve the same issue:
以下是我为解决同一问题所采取的步骤:
In the console I wrote
php artisan tinker
Then, again in the console,
Schema::drop('users')
At the end
php artisan migrate
and it all worked.
在我写的控制台中
php artisan tinker
然后,再次在控制台中,
Schema::drop('users')
最后
php artisan migrate
,一切都奏效了。
回答by ZAFIR AHMAD
Following command solved my problem:
以下命令解决了我的问题:
1. php artisan tinker
2. Schema::drop('your_table_name')
3. php artisan migrate
回答by Bùi Huy Th?ng
Here are the steps I took to solve the same issue:
以下是我为解决同一问题所采取的步骤:
php artisan make:migration create_tableName_table --create=tableName.
php artisan migrate.
appear error,you can drop all file in migration and all table in database.
create new table as 1.
finish. okay.
php artisan make:migration create_tableName_table --create=tableName.
php工匠迁移。
出现错误,您可以删除迁移中的所有文件和数据库中的所有表。
创建新表为 1。
结束。好的。
回答by suman
The simple way to solve this problem is run the following command
解决这个问题的简单方法是运行以下命令
php artisan migrate:fresh
php工匠迁移:新鲜
回答by Niya Bhayani
if (!Schema::hasTable('users')) {
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Use !Schema::hasTable('users'),It will check whether a table already exist in database or not. If you don't want to drop table then it is a good idea.
使用!Schema::hasTable('users'),它会检查一个表是否已经存在于数据库中。如果您不想删除表,那么这是一个好主意。
回答by Lucas Coelho
Migration errors usually occur when using php artisan migrate
and your migration has an error.
使用时通常会发生迁移错误,php artisan migrate
并且您的迁移有错误。
Within Laravel, there are some ways to reverse something that has been performed, especially migrations to more robust databases like MySql, for example.
在 Laravel 中,有一些方法可以逆转已执行的操作,尤其是迁移到更强大的数据库,例如 MySql。
One way to be reversing the step taken
扭转所采取步骤的一种方法
php artisan migrate
is to run the
是运行
php artisan migrate: rollback
It automatically reverses the last migrate performed You also have the possibility to delegate how many steps will be reversed with the step parameter.
它会自动反转上次执行的迁移您还可以使用 step 参数委派将要反转的步骤数。
The number says how many steps will be reversed
数字表示将反转多少步
php artisan migrate: rollback --step=1
回答by Mohsen
You can skip migrate the table if table exist in database by add this code:
如果数据库中存在表,您可以通过添加以下代码跳过迁移表:
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();
});
}
回答by Muhammad Maaz Haidari
Create your migrations by command php artisan make:migration create_category_table Then in your database/migrations set your necessary fields and databases,then run this command pho artisan migrate --pretend It will show sql statements,copy sql statement of category and paste it in database in sql and just click on run,your migrations would be there,no need to delete users table
通过命令 php artisan make:migration create_category_table 创建你的迁移然后在你的数据库/迁移中设置你需要的字段和数据库,然后运行这个命令 pho artisan migrate --pretend 它将显示 sql 语句,复制类别的 sql 语句并将其粘贴到数据库中在 sql 中,只需单击运行,您的迁移就会在那里,无需删除用户表
回答by besartm
Solution:
解决方案:
- Go on database -> phpmyadminif you are on localhost
- Delete everything on database that you created
- On cmd run
php artisan migrate
- 如果您在本地主机上,请继续数据库 -> phpmyadmin
- 删除您创建的数据库上的所有内容
- 在 cmd 上运行
php artisan migrate