Laravel 尝试迁移时出错

Laravel error while trying to migrate

我想向我的数据库 table 用户添加另一列。我做了一个名为 Modify_users_table 的新迁移,其中包含添加列的代码。源码没有问题,因为我之前用过,但是却报错table角色有问题?我该如何解决这个问题?这是错误和与错误相关的代码

这是迁移 Roles:

 public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->string('role_name')->length(55)->unique();
        $table->increments('id');
        $table->timestamps();
    });
}

你需要运行

php artisan migrate:rollback

如果这也失败了,只需进入并删除您可能必须做的所有 tables,因为看起来您的迁移 table 被搞砸了,或者您的角色 table 当您 运行 之前的回滚没有删除 table。

或者您可以使用 migrate:reset 命令将回滚您应用程序的所有迁移:

php artisan migrate:reset

从数据库中删除 table(我第一次遇到这个问题时手动完成)

并在 create_users_table 迁移中进行此更改

$table->string('email', 60)->unique();

这对我有用..

希望这对你也有帮助

你有很多问题。您的主要问题是您没有向 table 添加列。您的脚本试图创建一个新的 table,如以下行所示:

Schema::create('roles', function (Blueprint $table) {

您说您正在尝试向用户 table 添加一列,但您甚至没有使用用户的 table:而是创建了一个名为 table 的列roles 已经存在。最简单的解决方案是按照 Dhaval 的建议:放弃添加的迁移,更新创建用户 tables 的迁移,核弹并重新开始。

"right" 答案是创建一个新的迁移并放入将列添加到用户的说明 table 以及删除它的说明。然后你可以做一个简单的迁移而不是破坏你的数据库。那看起来像这样:

public function up()
{
    Schema::table('users', function ( Blueprint $table ) {
        $table->string('last_name')->after( 'first_name' )->default( '' );
    });
}

public function down()
{
    Schema::table( 'users', function ( Blueprint $table ) {
        $table->dropColumn( ['last_name'] );
    });
}

我遇到了同样的问题,我尝试了类似的方法,它非常适合我。我的 Laravel 版本是 5.5.

public function up()
{
    Schema::connection('mysql2')->create('images', function (Blueprint $table) {
        $table->increments('id');             
        $table->string('name');                      
        $table->string('image');            
        $table->timestamps();
    });
}

public function down()
{
    Schema::connection('mysql2')->dropIfExists('images');
}

我必须在我的项目中建立联系,一个是 mysql,另一个是 mysql2,因为我的项目用于具有多个连接的数据库。

所以我的解决方案是尝试在函数中指定与数据库的连接。

默认连接是mysql如果你没有在函数中指定它,例子如下。希望这对您有所帮助!

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');                
            $table->string('name');
            $table->string('email',60)->unique();
            $table->string('password');                
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }

因此,当您使用 php artisan 进行迁移时,它不会显示错误,