2017-02-10 41 views
2

我的单元测试最近开始失败。我收到此错误:Laravel表*没有列*

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数据库。

我在想,如果迁移是异步运行的,并且它们并没有全部由我的代码运行完成?或者如果迁移在某处失败了?

我一直在这个小时,不知道发生了什么。

*更新我在上面的迁移之后运行了一个迁移,我确认后续的迁移是成功的。因此,这只是一种以某种方式默默无闻的迁移。

+0

你看到,在迁移的运行列表中添加列在迁移? –

+0

是的,我列出了所有这些,包括我期望的。 – ajon

+0

并且您是否在数据库中看到该列?如果迁移正在运行时发生错误,则可能会将其保存在迁移表中,即使它尚未完成。 –

回答

4

我发现了这个问题。这是因为这个荒谬的限制,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'); 
}); 
+1

感谢您的回答。这有很大帮助。 你不能使用$ table-> morphs('taggable')是非常令人失望的;因为这会创建列和索引,这些在使用sqlite进行单元测试时会产生问题。 – Hendrik