【问题标题】:Error Users already exits when trying to create a column to an existing table尝试为现有表创建列时出现错误用户已存在
【发布时间】:2019-12-05 22:02:44
【问题描述】:

我正在尝试向现有表“用户”添加一个新列。我创建了一个新的迁移“add_privilege_column_to_users_table”。当我尝试运行 php artisan 迁移时,出现“用户”表已存在的错误。我在这里错过了什么吗?

我已经尝试了几种方法,来自谷歌搜索和其他在 starckoverflow 遇到相同问题的人。但是这里的代码都没有帮助我。

class AddPrivilegesColumnToUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('users', function (Blueprint $table) {

        $table->string('privilege_type')->after('remember_token');

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {

    });
}
}

PDOException::("SQLSTATE[42S01]: 基表或视图已存在:1050 表 'users' 已存在")

这是我在运行 php artisan migrate 时遇到的特定错误。我只是想在该表中添加一个新列。

以下是我的迁移状态:

Run? | Migration
No     2014_10_12_000000_create_users_table
No     2014_10_12_100000_create_password_resets_table
No     2019_07_28_071643_add_privileges_column_to_user_table

【问题讨论】:

  • 这是因为之前你会遇到错误,因此 Laravel 无法 drop() 表。你需要进入你的 mysql 和 DROP TABLE users 然后重新运行 php artisan migrate 命令确保你首先运行 migrate:rollback
  • 哦,但是我没有提到我已经在那个表中有数据了。如果我按照您的建议进行操作,那将删除我的数据,对吗?
  • 向我们展示您的php artisan migrate:status。看起来你回滚了太多,Laravel 认为你在某个时候删除了用户表并试图重建它。
  • 我能弄明白。事实证明我的桌子必须运行。根据我的迁移:状态,它说“不”在运行。大声笑

标签: php mysql laravel-5 database-migration


【解决方案1】:

您似乎回滚得太远了,但您的 users 表没有被删除,因此 Laravel 在尝试运行迁移时正在尝试重新创建表。

如果您前往您的数据库,您将能够更新迁移以说明它已经执行,从而跳过该批次。

UPDATE migrations SET batch = 1 WHERE migration = '2014_10_12_000000_create_users_table';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2015-01-15
    • 2016-07-12
    • 1970-01-01
    • 2019-04-22
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多