Laravel migration dropColumn 删除错误的列
Laravel migration dropColumn drop the wrong column
我有一个非常奇怪的问题,我无法解释。这让我很担心,因为我正在处理的项目是在线的,有时我可能会更新数据库列。
// Migration file
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->datetime('status_updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_profiles', function($table)
{
// Columns to remove
$table->dropColumn('status_updated_at');
});
}
}
现在我迁移了,我检查过一切正常,我的table是好的
html(master)$ php artisan migrate
Migrated: 2015_03_19_111236_change_user_profiles_table
这是我的一部分 MySQL table
现在我回滚,听起来很简单。
html(master)$ php artisan migrate:rollback
Rolled back: 2015_03_19_111236_change_user_profiles_table
现在我只要看看我的 table 就会变得怪异
是的,status
列无故消失了。而且还有一个应该被删除的。我试了 10 次都没用...我什至不能 migrate
因为
[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'status_
updated_at' (SQL: alter table `user_profiles` add `status_updated_at` datet
ime null)
有人对此有想法吗?如果这是一个问题,那就是一个大问题,因为迁移是项目中非常敏感的东西......我真的不再信任 Laravel,我想知道我将如何处理生产方面。
**编辑:要在此处找到解决方案,请与此 table 相关的所有迁移(但无论如何都不应该调用它...)
我正在使用 Laravel 4.2**
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_profiles', function($table)
{
// Keys
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->integer('box_id')->unsigned()->nullable();
$table->string('stripe_customer');
$table->string('contract_id');
// Indexes
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('box_id')->references('id')->on('boxes')->onDelete('cascade');
// Timestamps
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
$table->dropForeign('user_profiles_user_id_foreign');
$table->dropForeign('user_profiles_box_id_foreign');
});
Schema::dropIfExists('user_profiles');
}
}
以后
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->enum('status', array('not-subscribed', 'in-progress', 'subscribed', 'expired'));
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
// Columns to remove
$table->dropColumn('status');
});
}
}
我在一些迁移中使用了相同的 classe 名称,这让 composer 陷入了困境。 composer dump-autoload
让我看到了
例如:
Generating autoload files
Warning: Ambiguous class resolution, "ChangeBoxQuestionsTable" was found in both "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_12_183836_change_box_questions_table.php" and "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_19_040137_change_box_questions_table.php", the first will be used.
所以我手动更改了我的文件/类 名称以及数据库中的 migrations
table。现在它工作正常 ;)
我有一个非常奇怪的问题,我无法解释。这让我很担心,因为我正在处理的项目是在线的,有时我可能会更新数据库列。
// Migration file
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->datetime('status_updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_profiles', function($table)
{
// Columns to remove
$table->dropColumn('status_updated_at');
});
}
}
现在我迁移了,我检查过一切正常,我的table是好的
html(master)$ php artisan migrate
Migrated: 2015_03_19_111236_change_user_profiles_table
这是我的一部分 MySQL table
现在我回滚,听起来很简单。
html(master)$ php artisan migrate:rollback
Rolled back: 2015_03_19_111236_change_user_profiles_table
现在我只要看看我的 table 就会变得怪异
是的,status
列无故消失了。而且还有一个应该被删除的。我试了 10 次都没用...我什至不能 migrate
因为
[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'status_
updated_at' (SQL: alter table `user_profiles` add `status_updated_at` datet
ime null)
有人对此有想法吗?如果这是一个问题,那就是一个大问题,因为迁移是项目中非常敏感的东西......我真的不再信任 Laravel,我想知道我将如何处理生产方面。
**编辑:要在此处找到解决方案,请与此 table 相关的所有迁移(但无论如何都不应该调用它...)
我正在使用 Laravel 4.2**
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_profiles', function($table)
{
// Keys
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->integer('box_id')->unsigned()->nullable();
$table->string('stripe_customer');
$table->string('contract_id');
// Indexes
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('box_id')->references('id')->on('boxes')->onDelete('cascade');
// Timestamps
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
$table->dropForeign('user_profiles_user_id_foreign');
$table->dropForeign('user_profiles_box_id_foreign');
});
Schema::dropIfExists('user_profiles');
}
}
以后
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeUserProfilesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_profiles', function($table)
{
// Keys
$table->enum('status', array('not-subscribed', 'in-progress', 'subscribed', 'expired'));
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('user_profiles', function(Blueprint $table)
{
// Columns to remove
$table->dropColumn('status');
});
}
}
我在一些迁移中使用了相同的 classe 名称,这让 composer 陷入了困境。 composer dump-autoload
让我看到了
例如:
Generating autoload files
Warning: Ambiguous class resolution, "ChangeBoxQuestionsTable" was found in both "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_12_183836_change_box_questions_table.php" and "/Users/Loschcode/Dropbox/htdocs/projets/bordeaux_in_box_lo/html/app/database/migrations/2015_03_19_040137_change_box_questions_table.php", the first will be used.
所以我手动更改了我的文件/类 名称以及数据库中的 migrations
table。现在它工作正常 ;)