Foreign Key Constraint fails from unit tests
See original GitHub issueI 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:
- Created 7 years ago
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please close the issue 👍
@kingkoma, my guess is that you’re using django-webtest. Look here: https://django-simple-history.readthedocs.io/en/latest/reference.html#using-django-webtest-with-middleware