Php 向数据库表中添加新列时迁移失败-无需迁移
我刚刚学会了如何使用迁移。我成功创建了具有此架构的表:Php 向数据库表中添加新列时迁移失败-无需迁移,php,database,laravel,migration,laravel-5.4,Php,Database,Laravel,Migration,Laravel 5.4,我刚刚学会了如何使用迁移。我成功创建了具有此架构的表: /** * Run the migrations. * * @return void */ public function up() { Schema::create('units', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->
/**
* 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();
});
}
不幸的是,我错过了路径
列,所以我尝试添加它。首先,我在laravel告诉自己如何添加新列
从文件中:
模式facade上的table方法可用于更新现有的
桌子
我的尝试:
/**
* 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
之后,我没有得到任何要迁移的
我做错了什么?您需要首先回滚迁移,这将销毁最后一个包含所有数据的迁移表:
php artisan migrate:rollback
然后再次运行php artisan migrate
命令。这是应用程序开发过程中的最佳选择
如果您出于某种原因不想这样做(应用程序是live等),但只想添加一列,则创建一个新迁移并添加以下代码:
public function up()
{
Schema::table('units', function (Blueprint $table) {
$table->string('path');
});
}
并运行以下命令:
composer dumpauto
php artisan migrate
你必须为你的专栏更新准备一个新的迁移。或者,您可以回滚上一次迁移,添加缺少的列,然后重新运行迁移(假设尚未在生产环境中运行迁移)
回滚:php-artisan-migrate:rollback可以,但是php-artisan-migrate:rollback
会从表中删除所有现有数据吗?对吗?@EdwardBlack是第一个选择,是的。它将从上一次迁移中删除所有表。如果您不想这样做,请使用第二个选项。命令php artisan migrate:rollback
将只执行迁移文件中的down()
方法,这是否正确?如果我只是执行echo'hello'代码>并运行rollback命令,然后它在控制台中显示“hello”,因此我认为我是正确的。@EdwardBlack是的,但它将对上一批中的所有迁移执行此操作。