【问题标题】:Unable to add new column to a table using migration in laravel无法使用 laravel 中的迁移向表中添加新列
【发布时间】:2017-10-18 16:55:54
【问题描述】:

我正在使用 laravel 将列位置添加到用户表中。我创建了一个名为 2017_05_18_025207_add_username_field_to_users_table 的新迁移 .. 并在 up() 和 down() 中对其进行了描述架构和全部修改。但是,当我尝试使用 PHP artisan migrate 迁移它时。它没有反映在数据库中。

class AddUsernameFieldToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    { 
        Schema::table('users', function(Blueprint $table)
            {
                $table->string('place');
            }); 
    }

    /**
     * Reverse the migrations.
     *  
     * @return void
    */
    public function down()
    {
        Schema::table('users', function(Blueprint $table)
            {
                $table->dropColumn('place');
            });
    }
}

这段代码是我在新迁移中写的 .

【问题讨论】:

  • 您在尝试迁移时是否看到任何错误?运行后它是否也出现在迁移表中?
  • 不,它没有出现在我的迁移表中。并且没有发现错误。事实上,在 cmd 中我收到了这条消息 Migrating: 2017_05_18_025207_add_username_field_to_users_table Migrated: 2017_05_18_025207_add_username_field_to_users_table
  • 您用来连接数据库的任何客户端都可能存在缓存问题吗?尝试 sql 命令describe users; 以确保该列实际上没有被添加。
  • @user3158900 感谢您的帮助.. 它完成了.. 我不知道出了什么问题,但我回滚并尝试了 2 -3 次终于添加了该列。

标签: php mysql database laravel migration


【解决方案1】:

你需要运行composer du命令注册迁移类,然后才运行php artisan migrate命令。

【讨论】:

  • 它没有帮助:(
【解决方案2】:
$table->string('place',255);

运行 composer dump-autoload 并运行迁移。

【讨论】:

    猜你喜欢
    • 2013-05-23
    • 2021-05-13
    • 2019-04-16
    • 2014-12-26
    • 2015-09-22
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多