How do you fix a foreign key constraint that failed? Im trying to run a test but I get this message:
1) Tests\Feature\ShowIdeasTest::single_idea_shows_correctly_on_the_show_page
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed (SQL: insert into "ideas" ("user_id", "category_id", "title", "description", "slug", "updated_at", "created_at") values (1, Category 1, My First Idea, Description of my first idea, my-first-idea, 2021-10-12 23:23:12, 2021-10-12 23:23:12))
This is my test file:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Idea;
use App\Models\Category;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ShowIdeasTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function list_of_ideas_show_on_main_page()
{
$categoryOne = Category::factory()->create(['name' => 'Category 1']);
$categoryTwo = Category::factory()->create(['name' => 'Category 2']);
$ideaOne = Idea::factory()->create([
'title' => 'My First Idea',
'category_id' => $categoryOne->id,
'description' => 'Description of my first idea',
]);
$ideaTwo = Idea::factory()->create([
'title' => 'My Second Idea',
'category_id' => $categoryTwo->id,
'description' => 'Description of my second idea',
]);
$response = $this->get(route('idea.index'));
$response->assertSuccessful();
$response->assertSee($ideaOne->title);
$response->assertSee($ideaOne->description);
$response->assertSee($categoryOne->name);
$response->assertSee($ideaTwo->title);
$response->assertSee($ideaTwo->description);
$response->assertSee($categoryTwo->name);
}
/** @test */
public function single_idea_shows_correctly_on_the_show_page()
{
$categoryOne = Category::factory()->create(['name' => 'Category 1']);
$idea = Idea::factory()->create([
'title' => 'My First Idea',
'category_id' => $categoryOne->name,
'description' => 'Description of my first idea',
]);
$response = $this->get(route('idea.show', $idea));
$response->assertSuccessful();
$response->assertSee($idea->title);
$response->assertSee($idea->description);
$response->assertSee($categoryOne->name);
}
public function ideas_pagination_works()
{
$categoryOne = Category::factory()->create(['name' => 'Category 1']);
Idea::factory(Idea::PAGINATION_COUNT + 1)->create([
'category_id' => $categoryOne->id,
]);
$ideaOne = Idea::find(1);
$ideaOne->title = 'My first idea';
$ideaOne->save();
$ideaEleven = Idea::find(11);
$ideaEleven->title = 'My eleventh idea';
$ideaEleven->save();
$response = $this->get('/');
$response->assertSee($ideaOne->title);
$response->assertDontSee($ideaEleven->title);
$response = $this->get('/?page=2');
$response->assertSee($ideaEleven->title);
$response->assertDontSee($ideaOne->title);
}
}
This is the way my tables are set up:
2021_10_07_221217_create_categories_table
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
2021_10_08_221217_create_ideas_table
Schema::create('ideas', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('category_id')->constrained();
$table->string('title');
$table->string('slug')->nullable();
$table->text('description');
$table->timestamps();
});
My understanding is this:
First I have the categories table and then the ideas, when the code hits the ideas table and tries to assign something into this row: $table->foreignId('category_id')->constrained(); it should know that the value that I want assigned is an ```id`` but I don't understand what this error is trying to explain so these are my 3 questions regarding this error, does this mean that...........
- the row doesn't exist in the categories table?....
- that the value from the other related table (categories) has a wrong value assigned to it?
- or, that it needs to have an already saved value in order to pass this test?
As I been able to understand (a little) when a foreign key constraint fails is because a value from the related table, "55" cannot be inserted into another because there is no value of "55", therefore it fails, doesn't exist or is not the correct one.
Well if that is the case, it so happens that I can run php artisan migrate:fresh --seed locally and it will run the seed just fine, if I look at SequelPro I will have 30 records of ideas, 4 categories and 30 users, if I look at the ideas table in SequelPro i see that every and each idea has a category assigned from the categories, meaning the database relationships are good. This is also happening on my Heroku account, I deployed the website and when I try to run the same php artisan command I had the exact same error message. What part of this post is correct and what's not? Thank you.