SQLite 在 Migration 中 dropColumn 的一个小坑,哭死你
背景#
第一,我们测试的时候使用 SQLite。
第二,一个正在运行的网站,数据库的有一些改变,我们一般是会新添加 migration,如下:
public function up()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column1');
$table->integer('column2')->comment('.......');
//...等等等
});
}
public function down()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column2');
$table->tinyInteger('column1');
//...等等等
});
}
问题#
以上代码在 Mysql 上跑没有任何问题,不管是 migrate
还是 migrate:rollback
还是 migrate:refresh
,都完美执行。
但是在测试(使用 SQLite)的时候,各种诡异的问题,这个字段不存在,那个字段有问题...
解决方法#
每一个操作都放在一个 Schema::table 的闭包里面。
public function up()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column1');
});
Schema::table('table_name', function (Blueprint $table) {
$table->integer('column2')->comment('.......');
});
//...等等等
}
public function down()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column2');
});
Schema::table('table_name', function (Blueprint $table) {
$table->tinyInteger('column1');
});
//...等等等
}
好多人遇到这个问题,比如:
https://github.com/laravel/framework/issue...
如果你遇到了,不要大惊小怪。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: