Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

Tags:

php

mysql

laravel

I have a simple Article Model and a User Model.

Article "belongsTo" a User and a User "hasMany" Article.

Therefore my article migration has a foreign key called "user_id".

    Schema::create('articles', function(Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });

Yet whenever I create an Article where I pass the "user_id" in a hidden field I get an error message.

{!! Form::open(array('route' => 'articles.store')) !!}

    {!! Form::hidden('userId', $user->id) !!}

    <div class="form-group">
        {!! Form::label('title', 'Title') !!}
        {!! Form::text('title', null, array('class' => 'form-control')) !!}         
    </div>

    <div class="form-group">
        {!! Form::label('text', 'Write your Article') !!}
        {!! Form::textarea('text', null, array('class' => 'form-control')) !!}          
    </div>

    {!! Form::submit('Create Article', array('class' => 'btn btn-default btn-success')) !!}
{!! Form::close() !!}

This is the error message. And I understand that I doesn't try to insert the value for the 'user_id' into the articles table.

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (tags.articles, CONSTRAINT articles_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id)) (SQL: insert into articles (title, body, updated_at, created_at) values (Title, Some text, 2015-03-21 23:19:33, 2015-03-21 23:19:33))

Here is my Store Method on my AriclesController:

    Article::create([
        'title'   => Input::get('title'),
        'body'    => Input::get('text'),
        'user_id' => Input::get('userId')
    ]);

    return Redirect::to('articles');

There are dozens of other open Stackoverflow Questions with a similar title, and I am searching yet unsuccessfully for the answer that fits my specific case, therefore I thank you in advance kind stranger.

How do I save an article into my database?

like image 355
LoveAndHappiness Avatar asked Mar 21 '15 23:03

LoveAndHappiness


People also ask

What is a 1452 constraint violation in Twitter?

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( twitter. tweets, CONSTRAINT tweets_user_id_foreign FOREIGN KEY ( user_ id) REFERENCES users ( id) ON DELETE CASCADE)

How to fix error 1452 cannot add or update a child row?

If you are getting error Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails while using php artisan migrate command after creating a migration file. You can set foreign id column value as nullable with null on delete on delete parent table record.

Why do I get the above error after creating FOREIGN KEY constraint?

After creating foreign key constraint, whenever we insert records into the first table or child table, we will get the above error. The error comes when you are trying to add a row for which no matching row in in the other table.

What does MySQL error 1452 mean?

MySQL error 1452 - Cannot add or update a child row: a foreign key constraint fails? MySQL error 1452 - Cannot add or update a child row: a foreign key constraint fails? This error comes whenever we add a foreign key constraint between tables and insert records into the child table.


1 Answers

Make sure you have user_id in the fillable property of your Article model.

http://laravel.com/docs/5.0/eloquent#mass-assignment

like image 102
Adam Particka Avatar answered Sep 25 '22 00:09

Adam Particka