Help with this error SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update to child row: a foreign key constraint fails

2

Good I am trying to fill two tables of related data bases in Laravel using ajax

one of books

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

class CreateLibrosTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('libros', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('titulo');
        $table->integer('id_categoria')->unsigned();
        $table->integer('id_autor')->unsigned();
        $table->integer('id_editorial')->unsigned();
        $table->integer('cantidad');
        $table->date('fecha_lanzamiento');
        $table->string('idioma');
        $table->integer('paginas');
        $table->string('descripcion');
        $table->string('tipo');
        $table->string('enlace');

        $table->timestamps();
    });
}

and one of categories

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

class CreateCategoriasTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('categorias', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('nombre');
        $table->integer('libro_id')->unsigned();
        $table->foreign('libro_id')->references('id')->on('libros');


        $table->timestamps();
    });
}

and the AJAX scrip

$("#registro").click(function()
{
var dato = $("#categoria").val();
var route = "http://localhost:8000/categoria";
var token = $("#token").val();

$.ajax({
    url: route,
    headers:{'X-CSRF-TOKEN': token},
    type: "POST",
    dataType:'json',
    data: {categoria: dato},

    success:function(){
        $("#msj-success").fadeIn();
    },

    error:function(msj){
        $("#msj").html(msj.responseJSON.categoria);
        $("#msj-error").fadeIn();
    }

});

});

at the time of trying to fill the tabala categories I get the following error:

QueryException in Connection.php line 624: SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update to child row: a foreign key constraint fails ( biblioteca . categorias , CONSTRAINT categorias_libro_id_foreign FOREIGN KEY ( libro_id ) REFERENCES libros ( id )) (SQL: insert into categorias ( updated_at , created_at ) values (2017-01-05 19:06:28, 2017-01 -05 19:06:28))

If someone can identify what I am doing wrong, I would appreciate it if you mentioned it

    
asked by Santiago Avila 06.01.2017 в 00:44
source

2 answers

1

First, I think you have the FK inverted. A book belongs to a category, not the other way around (unless it was a many-to-many relationship that is not the case).

Second, given the relationship of your question, you can not insert a category without specifying the id of the book.

I do not know specifically how it is done in illuminate to set the default value of book_id to NULL, which would allow inserting the category if book_id and without violating the FK.

    
answered by 15.02.2017 в 03:15
0

Verify the condition of the foreign key, possibly trying to record in the detail table before recording in the main table.

    
answered by 06.01.2017 в 00:49