SQLSTATE [23000]:违反完整性约束:19 NOT NULL约束失败

时间:2018-08-20 22:05:43

标签: php database laravel sqlite phpunit

我正在Laravel中编写一个测试以创建图钉。创建图钉时,我想将链接保存到它自己的数据库表中。这是测试:

/** @test */
function an_authorized_user_can_create_a_pin()
{
    $this->withoutExceptionHandling();
    $user = create(User::class);
    $this->be($user);

    $pin = make(Pin::class);

    $response = $this->post('/pin', $pin->toArray());

    $this->assertDatabaseHas('pins', [
        'user_id' => $user->id,
        'title' => $pin->title,
    ])
    ->assertDatabaseHas('links', [
        'link' => $pin->link,
        'pin_id' => $createdPin->id
    ]);
}

测试失败,并显示以下错误:

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity 
constraint violation: 19 NOT NULL constraint failed: links.link (SQL: 
insert into "links" ("link", "pin_id", "updated_at", "created_at") 
values (, 1, 2018-08-20 21:44:19, 2018-08-20 21:44:19))

这是我的控制者:

public function store(Request $request)
{
    $pin = new Pin();
    $pin->user_id = auth()->id();
    $pin->title = $request->title;
    $pin->save();

    $pinLink = new Link();
    $pinLink->link = $request->link;
    $pinLink->pin_id = $pin->id;
    $pinLink->save();
}

这两个工厂都在这里:

<?php

use App\Pin;
use Faker\Generator as Faker;

$factory->define(Pin::class, function (Faker $faker) {
    return [
        'user_id' => function () {
            return factory('App\User')->create()->id;
        },
        'title' => $faker->sentence(20),
    ];
});

<?php

use App\Pin;
use Faker\Generator as Faker;

$factory->define(Pin::class, function (Faker $faker) {
    return [
        'user_id' => function () {
            return factory('App\User')->create()->id;
        },
        'title' => $faker->sentence(20),
    ];
});

这是我不太确定要做什么的地方,因为我是PHP和Laravel的新手(也许还有Mysql / PDO之类的东西,渴望学习)。

0 个答案:

没有答案