That is a common problem, you are probably hard coding a foreign key somewhere in your tests, instead of storing the one you want in a variable, when you create the row.
Integrity Constraint Violation Errors in Feature Tests for Build A Voting App
I finished the Build A Voting App series but keep getting integrity constraint violation errors when running a number of the feature tests. All of the errors relate to the status_id foreign key; 7 of the 8 errors occur when trying to insert into the comments table while the other error relates to trying to insert into the ideas table.
Here's an example of one of them when trying to insert into the comments table (and here's the test code: https://github.com/laracasts/lc-voting/blob/master/tests/Feature/Comments/AddCommentTest.php):
1) Tests\Feature\Comments\AddCommentTest::add_comment_form_works
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`testing`.`comments`, CONSTRAINT `comments_status_id_foreign` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`)) (SQL: insert into `comments` (`user_id`, `idea_id`, `status_id`, `body`, `updated_at`, `created_at`) values (79, 50, 1, This is my first comment, 2022-11-28 14:06:17, 2022-11-28 14:06:17))
And here's what I get for the ideas table (and here's the test code: https://github.com/laracasts/lc-voting/blob/master/tests/Feature/CreateIdeaTest.php):
9) Tests\Feature\CreateIdeaTest::creating_an_idea_works_correctly
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`testing`.`ideas`, CONSTRAINT `ideas_status_id_foreign` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`)) (SQL: insert into `ideas` (`user_id`, `category_id`, `status_id`, `title`, `description`, `slug`, `updated_at`, `created_at`) values (185, 84, 1, My First Idea, This is my first idea, my-first-idea, 2022-11-28 14:06:19, 2022-11-28 14:06:19))
Interestingly, I only get the errors when I run the full suite of tests. If I run each test individually I do not get the errors. Any suggestions?
@georgetown74 It doesn't clear anything, it does a rollback.
The first time it runs, it migrates the database, and then it starts a transaction, performs the test, rolls back the data to its initial state, and then creates a new transaction and performs the next test.
I just did a test with this code in Tinkerwell and each time I run it the id increases.
use App\Models\Author;
DB::transaction(function () {
$id = Author::factory() ->create();
echo $id;
DB::rollback();
});
{"first_name":"Ella","last_name":"Connelly","updated_at":"2022-11-29T17:36:25.000000Z","created_at":"2022-11-29T17:36:25.000000Z","id":507}
{"first_name":"Bennett","last_name":"Spinka","updated_at":"2022-11-29T17:38:00.000000Z","created_at":"2022-11-29T17:38:00.000000Z","id":508}
So that is why your tests fail when using MySQL.
Please or to participate in this conversation.