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


Laravel seeder integrity constraint violation 1452

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

完整性约束违反1452无法添加或更新子行:外键约束失败("2016"、"products"、constraint'products_user_id_eforeign'FOREING KEY('user_id')REFERENCES'users'('id')删除级联)。

迁移效果良好。播种机信息编号

移植顺序项目:

<?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,
        ]);


    }
}

我打电话给播种机:

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

此错误是由于id为1的用户不存在。

首先创建用户表种子示例:

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

现在对产品表进行种子处理