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
, CONSTRAINTarticles_user_id_foreign
FOREIGN KEY (user_id
) REFERENCESusers
(id
)) (SQL: insert intoarticles
(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?
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)
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.
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.
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.
Make sure you have user_id
in the fillable
property of your Article model.
http://laravel.com/docs/5.0/eloquent#mass-assignment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With