lundi 2 mai 2016

Laravel seeder integrity constraint violation 1452

I created table Users, Products, Oders, Oder_Items. this is for a ecommerce, i want that each product have a author (user). Artisan give a error :

integrity constraint violation 1452 Cannot add or update a child row: a foreign key constraint fails ('2016', 'products', CONSTRAINT 'products_user_id_foreign' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE).

Migration work good. Seeder information no.

MIGRATION ORDERS_ITEMS:

<?php

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

class CreateOrderItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_items', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('price', 5, 2);
            $table->integer('quantity')->unsigned();






            //--------// ogni item ha un prodotto ID
            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')
                  ->references('id')
                  ->on('products')
                  ->onDelete('cascade');

            // Ogni item ha un ORDER ID, cosi possiamo filtrare tutti gli item degli ordini   
            $table->integer('order_id')->unsigned();
            $table->foreign('order_id')
                  ->references('id')
                  ->on('orders')
                  ->onDelete('cascade');;
         });





    }

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

MIGRATION ORDERS

<?php

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

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('subtotal', 5, 2);
            $table->decimal('shipping', 5,2);



            $table->timestamps();  





         });
    }

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

MIGRATION PRODUCTS

<?php

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

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //Up creare table
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug'); 
            $table->text('description');
            //mostrare una piccola descrizione del prodotto
            $table->string('extract', 300);
            $table->decimal('price', 5, 2);
            $table->string('image', 300);
            //vedere se pubblico o no
            $table->boolean('visible');
            // unsigned solo valori positivi
            //e fa riferimento al id category, 
            //Se si cancella, cancellerà tutti i prodotti con quella categoria
            //Ogni prodotto ha una categoria

            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');







            $table->integer('category_id')->unsigned();

            // relazioni  
            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onDelete('cascade');
            //crea // Ogni prodotto ha un autore( artista)




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

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

MIGRATION USERS

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
   public function up()

{

    Schema::create('users', function (Blueprint $table) {

        $table->increments('id');

        $table->string('name');

        $table->string('lastname');

        $table->string('username');

        $table->string('birth');

        $table->string('profile');

        $table->string('country');

        $table->string('province');

        $table->string('address');

        $table->string('address2');

        $table->string('phone');

        $table->string('usertype');

        $table->string('email')->unique();

        $table->string('password', 60);

        $table->boolean('social');

        $table->boolean('active')->default(0);

        $table->string('confirm_token', 100);

        $table->rememberToken();

        $table->timestamps();

    });

}






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

PRODUCTS SEEDER

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

//Con questo gli dico di usare il mio modello per questo SEEDER
use dixard\Product;

class ProductTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {



            DB::table('products')->insert([
            'name'          => 'Playera 1',
                'slug'          => 'playera-1',
                'description'   => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus repellendus doloribus molestias odio nisi! Aspernatur eos saepe veniam quibusdam totam.',
                'extract'       => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
                'price'         => 275.00,
                'image'         => 'http://ift.tt/1SX3Wjc',
                'visible'       => 1,

                'created_at'    => new DateTime,
                'updated_at'    => new DateTime,
                'category_id'   => 1,
                'user_id'   => 1,
        ]);




    }
}

I CALLED SEEDER :

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        // $this->call(UserTableSeeder::class);
        $this->call(CategoryTableSeeder::class);
        $this->call(ProductTableSeeder::class);


        Model::reguard();
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire