我刚刚学会了如何使用迁移。我成功地用这个模式创建了一个表:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
不幸的是,我错过了 path
列,所以我尝试添加它。首先我在 laravel 中告诉自己 documentation了解如何添加新列。
来自文档:
The table method on the Schema facade may be used to update existing tables.
我的尝试:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
Schema::table('units', function (Blueprint $table) {
$table->string('path');
});
}
但是,在执行 php artisan migrate
之后,我得到了 Nothing to migrate
。
我做错了什么?
最佳答案
您需要先回滚迁移,这将破坏包含所有数据的最后迁移表:
php artisan migrate:rollback
然后再次运行 php artisan migrate
命令。这是开发应用程序时的最佳选择。
如果您出于某种原因不想这样做(应用程序已上线等),但只想添加一个列,然后创建一个新的迁移并添加此代码:
public function up()
{
Schema::table('units', function (Blueprint $table) {
$table->string('path');
});
}
然后运行这些命令:
composer dumpauto
php artisan migrate
关于php - 将新列添加到数据库表中,迁移失败并显示 - 无需迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42485951/