我有这些迁移:
class CreateOrdersTable extends Migration
{
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
}
class CreatePaymentsTable extends Migration
{
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders');
});
}
}
这些工厂:
$factory->define(Payment::class, function (Faker $faker) {
return [
'order_id' => factory(Order::class)->create(),
];
});
$factory->define(Order::class, function (Faker $faker) {
return [
'user_id' => factory(User::class)->create(),
];
});
现在在我的测试中我有这个:
/** @test */
public function it_should_count_1_order()
{
$order = factory(Order::class)->create();
$payment = factory(Payment::class)->create([
'order_id' => $order->id,
]);
$this->assertEquals(1, Order::count())
}
Order 表计数给了我2
。为什么?应该是1
因为我告诉支付工厂order_id
用给定的订单覆盖。我错过了什么吗?