最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Integrity constraint violation while testing Laravel api routes using sanctum - Stack Overflow

programmeradmin2浏览0评论

I've a Laravel app with api routes using Sanctum authentication. When I try to write a feature test for any api route, I always receive an error:

"message": "SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed (SQL: insert into \"personal_access_token_logs\" (\"personal_access_token_id\", \"controller\", \"action\", \"request_ip\", \"request_uri\", \"request_method\", \"request_body\", \"response_body\", \"response_status\", \"updated_at\", \"created_at\") values (0, {hidden_controller}, show, ?, http://localhost/api/{hidden_route}, GET, [], {\"id\":84,\"personal_acc"...

I think it's related to the personal_access_token_id field having value 0, but I can't realize why this is happening.

I'm using sqlite with in_memory database, but changed to mysql with a testing database (and without cleaning before tests) to try to find what is happening. Same problem, user and token are created, but the error appears when try to log the connection into personal_access_token_logs.

The test code for references:

it('can show {hidden_route}', function () {
    $user = User::factory()->create(['is_admin' => true]);

    Sanctum::actingAs($user, ['*']);

    $response = $this->getJson('/api/{hidden_route}');
    $response->assertStatus(200);
}

I've a Laravel app with api routes using Sanctum authentication. When I try to write a feature test for any api route, I always receive an error:

"message": "SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed (SQL: insert into \"personal_access_token_logs\" (\"personal_access_token_id\", \"controller\", \"action\", \"request_ip\", \"request_uri\", \"request_method\", \"request_body\", \"response_body\", \"response_status\", \"updated_at\", \"created_at\") values (0, {hidden_controller}, show, ?, http://localhost/api/{hidden_route}, GET, [], {\"id\":84,\"personal_acc"...

I think it's related to the personal_access_token_id field having value 0, but I can't realize why this is happening.

I'm using sqlite with in_memory database, but changed to mysql with a testing database (and without cleaning before tests) to try to find what is happening. Same problem, user and token are created, but the error appears when try to log the connection into personal_access_token_logs.

The test code for references:

it('can show {hidden_route}', function () {
    $user = User::factory()->create(['is_admin' => true]);

    Sanctum::actingAs($user, ['*']);

    $response = $this->getJson('/api/{hidden_route}');
    $response->assertStatus(200);
}
Share Improve this question edited Jan 20 at 12:48 matiaslauriti 8,0824 gold badges36 silver badges47 bronze badges asked Jan 20 at 10:34 Eduardo CorralesEduardo Corrales 231 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

try to create token first

  $token = $user->createToken('Test Token')->plainTextToken;

in your code

it('can show {hidden_route}', function () {
$user = User::factory()->create(['is_admin' => true]);
$token = $user->createToken('Test Token')->plainTextToken;
Sanctum::actingAs($user, ['*']); 
$response = $this->getJson('/api/{hidden_route}');
$response->assertStatus(200);

});

I found the solution. There was a middleware LogApiAccess trying to log the request and response to database. I just disable it and the test worked.

$this->withoutMiddleware(LogApiAccess::class);
发布评论

评论列表(0)

  1. 暂无评论