Veneta
Veneta

Reputation: 33

Django REST with pytest fixture integrity error

I am trying to create a fixture with pytest, but when I run it, I get the following error:

django.db.utils.IntegrityError: NOT NULL constraint failed: authtoken_token.user_id

I found information on it, but I am not sure how to handle it when it comes to fixtures.

My code:

@pytest.fixture
def client(db):
    api_client = APIClient()
    token = Token.objects.get_or_create(user__username='testuser')
    api_client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)
    return api_client


@pytest.mark.django_db
def test_get_list_test(client):
    url = reverse('api/lists')
    response = client.get(url)
    assert response.status_code == 200

Upvotes: 0

Views: 437

Answers (1)

Moshe
Moshe

Reputation: 521

Generally speaking, i don't think your error related to pytest here. I think the main issue here related to django db migrations look here

So first, Make sure you fix it, regardless to any framework (pytest)

django.db.utils.IntegrityError: NOT NULL constraint failed: authtoken_token.user_id

And after you have successfully validated that the error passed away (check manually also), try to run the fixture.

Upvotes: 1

Related Questions