当我运行命令 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 auto_increment primary key,name
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)
[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
我在 Windows 上使用 Laravel 5.4。
最佳答案
它让您知道“用户”表已经存在 - 当您运行迁移时 - 它正在尝试创建该表(但它已经存在)
这通常是因为您之前尝试过运行命令“php artisan migrate”。必须回滚才能“撤消”这些更改,或者清除数据库表。
您可以运行:
php artisan migrate:rollback
这应该删除所有的表 - 然后你就可以运行
php artisan migrate
它应该正确加载所有内容。
替代方法?登录到您的数据库并从数据库中物理删除这些表。然后您可以再次运行迁移,它就会起作用。
一个小问题:首先检查您的迁移:它应该具有此功能:
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
如果没有,回滚不会删除用户表,您仍然会遇到此问题(这意味着您必须登录数据库并手动删除表)。
如果您使用此命令创建迁移,回滚功能将自动包含:
php artisan make:migration create_yourtablename_table
关于PHP artisan migrate 命令出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677019/