PROBLEM RELATIONSHIPS LARAVEL TABLES

1

I need your help, I am making a list of tables in laravel with migration, which tables are 'Products', 'inputs' and 'ProductProduct', the relationship or database would be from 1 to many (1 product has N inputs) but this error appears:

  

1215 Can not add foreign key constraint

Sent them the migration code

Product Migration

 public function up()
    {
        Schema::create('productos', function (Blueprint $table) {
            $table->increments('id');
            $table->String('nombreP')->unique();
            $table->String('categoriaP');
            $table->integer('valorP');
            $table->String('UmedidaP');
            $table->timestamps();
            
        });
    }

migration inputs

    public function up()
    {
        Schema::create('insumos', function (Blueprint $table) {
            $table->increments('id');
            $table->String('nombreI')->unique();
            $table->String('categoriaI');
            $table->integer('valorI');
            $table->String('UmedidaI');
            $table->timestamps();
        });
    }

Detalleproducto migracion

    public function up()
    {
        Schema::create('detalleproductos', function (Blueprint $table) {
            $table->increments('id');
            $table->Integer('id_producto');
            $table->Integer('id_insumo');
            $table->timestamps();

            //relacion
            $table->foreign('id_producto')->references('id')->on('productos');
            $table->foreign('id_insumo')->references('id')->on('insumos');
            $table->primary(['id_insumo', 'id_producto']);
        });
    }
    
asked by Matias Muñoz 04.11.2018 в 22:11
source

1 answer

1

I make the following observations:

  
  • The methods must be written in lowercase, that is, it is not the same to put String() as string() then always these in   lowercase
  •   
  • I have generated the foreign keys through the migrations, without needing this line so try removing it $table->primary(['id_insumo', 'id_producto']);
  •   
  • The files that contain each of these migrations, must be 3 in total and must be in the following order, verify that   be
  •   
  • The columns that you are going to use as foreign keys, must have the following structure $table->integer('columnaNombre')->unisgned()
  •   

    LISTING

  • products
  • supplies
  • product details
  • If for some reason you already created some of the tables, execute the following command to execute all the migrations again

    php artisan migrate:fresh
    

    CODE

    FIRST MIGRATION

     public function up()
        {
            Schema::create('productos', function (Blueprint $table) {
                $table->increments('id');
                $table->string('nombreP')->unique();
                $table->string('categoriaP');
                $table->integer('valorP');
                $table->string('UmedidaP');
                $table->timestamps();
    
            });
        }
    

    SECOND MIGRATION

        public function up()
        {
            Schema::create('insumos', function (Blueprint $table) {
                $table->increments('id');
                $table->string('nombreI')->unique();
                $table->string('categoriaI');
                $table->integer('valorI');
                $table->string('UmedidaI');
                $table->timestamps();
            });
        }
    

    THIRD MIGRATION

      public function up()
        {
            Schema::create('detalleproductos', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('id_producto')->unsigned();
                $table->integer('id_insumo')->unsigned();
                $table->timestamps();
    
                //relacion
                $table->foreign('id_producto')->references('id')->on('productos');
                $table->foreign('id_insumo')->references('id')->on('insumos');
            });
        }
    
        
    answered by 05.11.2018 / 02:40
    source