2016-05-02 18 views
-1

私は、ユーザー、製品、Oder、Oder_Itemsテーブルを作成しました。 これはeコマースのためのものですが、私は各製品に著者(ユーザ)がいなければなりません。 職人がエラーを与える:Laravelシーダー整合性制約違反1452

整合性制約違反1452は、子行を追加または更新できません: を外部キー制約が( '2016'、 '製品'、CONSTRAINT 'products_user_id_foreign' FOREIGN KEY( 'USER_ID' を失敗します)参照 'ユーザー' ( 'id')ON DELETE CASCADE)。

移行作業が良好です。シードャー情報番号

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

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

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

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

製品のシーダー

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

製品テーブルをシードあなたは、ID = 1を持つすべてのユーザーを作成しています –

答えて

0

このエラーは、ID = 1が存在しないと、ユーザーによるきます。

は、最初のユーザーテーブルシード 例を作成します。

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

は今