我的单元测试最近开始失败。我收到此错误:
PDOException: SQLSTATE[HY000]:
General error: 1 table loan_details has no column named start_month
它发生的行,我有这个代码:
$loan = LoanDetails::create(['loan_percentage' => .8,
'loan_product_id' => 1,
'interest_rate' => .5,
'start_month' => 0,
'term' => 120,
'fixed_finance_fee' => 0,
'variable_finance_Fee' => 0,
'valid_from' => '2015-01-01'
]);
如果我注释掉“start_month”行,那么它在逻辑上是有效的。
在我的单元测试设置中,我运行了所有迁移(大约 80 个)。
我的迁移如下所示:
Schema::table('loan_details', function(Blueprint $table){
$table->integer('start_month')->unsigned()->after('interest_only')->default(0);
$table->decimal('balloon_percent',4,3)->after('term')->nullable();
$table->integer('balloon_month')->after('balloon_percent')->nullable();
$table->dropColumn('ordinal_rank');
});
所以,我想知道是否所有迁移都没有运行,所以我运行了这段代码:
$rows = DB::table('migrations')->get();
print_r($rows);
这会列出所有已完成的迁移。我正在使用内存中的 sqlite 数据库进行测试。
我想知道迁移是否是异步运行的,并且在我的代码运行时它们还没有全部完成?或者如果迁移在某个地方默默地失败了?
我已经在这里呆了几个小时了,但不知道发生了什么。
*更新我有一个在上述迁移之后运行的迁移,并且我确认后续迁移成功。所以这只是一次迁移在某种程度上默默地失败了。
我发现了问题。这是因为 sqlite 有一个荒谬的限制,即在一个表调用中没有多个添加列语句,如here所示。
当我将迁移分开时,它会起作用:
Schema::table('loan_details', function(Blueprint $table){
$table->integer('start_month')->unsigned()->after('interest_only')->default(0);
});
Schema::table('loan_details', function(Blueprint $table){
$table->decimal('balloon_percent',4,3)->after('term')->nullable();
});
Schema::table('loan_details', function(Blueprint $table){
$table->integer('balloon_month')->after('balloon_percent')->nullable();
});
Schema::table('loan_details', function(Blueprint $table){
$table->dropColumn('ordinal_rank');
});
要检查 SQLite DB 记录这里
还有许多其他有用的内置点命令 - 请参阅以下位置的文档 http://www.sqlite.org/sqlite.html,sqlite3 的特殊命令部分。
另外,检查数据库架构
在我的例子中,我试图在同一个
Scheme::table
调用中向表中添加一列(在另一列之后)并删除一列,这在 2024 年 SQLite 显然无法处理。
Schema::table("transactions", function (Blueprint $table) {
$table->after("id", function (Blueprint $table) {
$table->string("model", 20)->nullable();
});
$table->dropColumn("payment_gateway");
});
我像这样分开通话:
Schema::table("transactions", function (Blueprint $table) {
$table->after("manufacturer", function (Blueprint $table) {
$table->string("model", 20)->nullable();
});
});
Schema::table("transactions", function (Blueprint $table) {
$table->dropColumn("payment_gateway");
});
}
...稍微重命名了迁移,然后再次运行它,错误不再出现在我的测试中。