question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foreign Key Constraint fails from unit tests

See original GitHub issue

I have some unit tests that create and modify models in the test database, and I get FK constraint failures when the test saves a new model instance:

IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_apptest`.`apptest_historicalprofile`, CONSTRAINT `apptest_history_user_id_34e23bc9_fk_auth_user_id` FOREIGN KEY (`history_user_id`) REFERENCES `auth_use)')

I’m using the middleware-based solution to collect the current user, which I guess is part of the reason for this issue (no request is made in the test, so no user to capture), but it looks like the historical* object allows a Null for the user_id, so what is happening here? And what is the workaround?

(Django 1.9.5, django-simple-history 1.8.0)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
gabn88commented, May 10, 2017

Please close the issue 👍

1reaction
rossmechaniccommented, Jul 16, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

FOREIGN KEY constraint failed in SQLite unit tests
Restrict to avoid a multiple cascade paths error on database creation. This is working as I expected when running locally using IIS Express...
Read more >
Unit test are not working without modification if generate entity ...
All unit tests fail and get the error Microsoft.Data.Sqlite.SqliteException : SQLite Error 19: 'FOREIGN KEY constraint failed'. image.png.
Read more >
Foreign Key Constraint fails from unit tests · Issue #216 - GitHub
I have some unit tests that create and modify models in the test database, and I get FK constraint failures when the test...
Read more >
Test is failing due to foreign key constraint failure - Laracasts
As I been able to understand (a little) when a foreign key constraint fails is because a value from the related table, "55"...
Read more >
[Fixed]-FOREIGN KEY constraint failed in SQLite unit tests
This is working as I expected when running locally using IIS Express and Visual Studio's SQL Server in memory, but throwing an error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found