1

While Inserting data to mysql database i'm getting the error coming below, and as of some previous question's answer on stackoverflow i put protected $fillable in my model file but still showing error.

when i am creating my first entry table its accepting without any error but when i'm making my new entry its showing error.

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`app`.`articles`, CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`article_id`) REFERENCES `users` (`id`)) (SQL: insert into `articles` (`article_head`, `article_body`) values (abkskfbasfbv, zbfvaksdvaskdvjsdc
))

Article.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
  protected $fillable = [
      'article_head', 'article_body',
  ];
    public $timestamps = false;
}

migration file

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateArticlesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function(Blueprint $table)
        {
            $table->increments('article_id');
            $table->string('article_head', 500);
            $table->string('article_body', 10000);
            $table->dateTime('created_on');
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('articles');
    }

}

2016_04_27_181606_add_foreign_keys_to_articles_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class AddForeignKeysToArticlesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('articles', function(Blueprint $table)
        {
            $table->foreign('article_id', 'articles_ibfk_1')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT');
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('articles', function(Blueprint $table)
        {
            $table->dropForeign('articles_ibfk_1');
        });
    }

}

Jatin Balodhi
  • 152
  • 5
  • 18
  • 1
    Possible duplicate of [Integrity constraint violation: 1452 Cannot add or update a child row:](http://stackoverflow.com/questions/14063652/integrity-constraint-violation-1452-cannot-add-or-update-a-child-row) – e4c5 Apr 28 '16 at 16:35
  • so do i need to add id column to my articles table same as users table? – Jatin Balodhi Apr 28 '16 at 16:50

2 Answers2

1

Put

protected $primaryKey = "article_id"; 

in your Article model.

oseintow
  • 7,221
  • 3
  • 26
  • 31
  • no its not working, when i am creating my first entry to article table its accepting without any error but when i'm making my new entry its showing errors – Jatin Balodhi Apr 28 '16 at 19:16
0

It just simply means that the value for article_id on table articles you are inserting doesn't exist on table users or you are not inserting value for articles on table users. Bare in mind that the values of column article_id on table users is dependent on the values of ID on table users

Panayiotis Georgiou
  • 1,135
  • 3
  • 19
  • 36