Laravel播种机完整性约束违规1452

时间:2016-05-02 09:34:57

标签: php mysql laravel laravel-5.1

我创建了表Users,Products,Oders,Oder_Items。 这是一个电子商务,我希望每个产品都有一个作者(用户)。 工匠给出错误:

  

完整性约束违规1452无法添加或更新子行:   外键约束失败('2016','产品',CONSTRAINT   'products_user_id_foreign'FOREIGN KEY('user_id')参考'用户'   ('id')ON DELETE CASCADE)。

迁移工作做得很好。播种机信息号

移民订单_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');
    }
}

移民订单

<?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');
    }
}

移民产品

<?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');
    }
}

移民使用者

<?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');
    }
}

产品种子

<?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://www.truffleshuffle.co.uk/store/images_high_res/Mens_Red_Batman_Graffiti_Logo_T_Shirt_hi_res.jpg',
                'visible'       => 1,

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




    }
}

我打电话给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();
    }
}

1 个答案:

答案 0 :(得分:0)

此错误是由于id = 1的用户不存在而引起的。

首先创建一个用户表种子 例如:

   DB::table('users')->insert([
            'name' => 'hero',
            'lastname' => 'power',
             ....
             ..
            ..
        ]);

现在播种产品表