ohffs's avatar
Level 50

Code fails in test - looks like model binding?

I have a basic bit of code that lets you pick from a drop-down which then gets used as a foreign key on the model. I've got a test which should pass, but I keep getting an error :

1) CourseTest::test_can_edit_a_valid_course
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add 
or update a child row: a foreign key constraint fails (`homestead`.`courses`, CONSTRAINT 
`courses_discipline_id_foreign` FOREIGN KEY (`discipline_id`) REFERENCES `disciplines` (`id`))
 (SQL: insert into `courses` (`code`, `title`, `discipline_id`, `updated_at`, `created_at`) 
values (TEST3985, quos sit ullam, {"code":"TEST_odit","title":"et nesciunt voluptas","updated_at":"2016-06-01
 12:41:55","created_at":"2016-06-01 12:41:55","id":22}, 2016-06-01 12:41:55, 2016-06-01 12:41:55))

It looks to me like it's setting the "discipline_id" to be an actual copy of the "discipline" model entry rather than just it's 'id'. The code works fine in a browser, but fails like that with phpunit. The controller action is a simple :

    public function update(Request $request, $id)
    {
        $course = Course::findOrFail($id);
        $course->fill($request->all())->save();
        return redirect()->route('course.show', $id);
    }

There's no relations set on the models yet. The html for the select to pick the discipline_id is just :

                <div class="form-group">
                    <select class="form-control" name="discipline_id">
                        @foreach ($disciplines as $discipline)
                            <option value="{{ $discipline->id }}" @if ($course->discipline_id == $discipline->id) selected @endif>{{ $discipline->title }}</option>
                        @endforeach
                    </select>
                </div>

Anyone else hit this? It's a bit puzzling... Edit: this is a L5.2 app.

0 likes
4 replies
ohffs's avatar
Level 50

'Doh! Never mind - it was actually my model factory that I'd made a mistake in :-/ I had :

$factory->define(App\Course::class, function (Faker\Generator $faker) use ($factory) {
    return [
        'code' => 'TEST' . $faker->randomNumber(4),
        'title' => implode(" ", $faker->words(3)),
        'discipline_id' => $factory->create(App\Discipline::class),
    ];
});

When the discipline_id factory call should have had a trailing ->id. There's an hour of my life I'll never get back... The benefits of going for a quick walk when you're stuck on something can't be under-estimated... :-)

1 like
SaeedPrez's avatar

This is the kind of thing that makes you grow thick manly hair on your chest, so it's all good :)

ohffs's avatar
Level 50

@SaeedPrez It certainly made me want to slap myself ;-) Almost as soon as I walked away from the keyboard after posting the question I had a big 'oh ffs!' moment (aptly) ;-) Anyway - it's freakishly sunny today so that's enough coding for me. I think it's time to collapse on the sofa with some podcasts and throw bouncy balls for my cat to chase.

Hardcore ;-)

SaeedPrez's avatar

@ohffs I seriously believe there is something in our brain that goes off after we post a question :) Have an awesome day buddy.

2 likes

Please or to participate in this conversation.