4

I have problem with my seeds. Here is my structure of my tables:

 1.Complaints:

 Schema::create('complaints', function (Blueprint $table) {
        $table->uuid('id');
        $table->unsignedInteger('origin_id');     
        $table->timestamps();

        $table->primary('id');
    });

  2.Complaint_bill

  Schema::create('complaint_bills', function (Blueprint $table) {
        $table->uuid('complaint_id');
        $table->string('symbol')->nullable();
        $table->string('item_name');
        $table->timestamps();

        $table->primary('complaint_id');

        $table->foreign('complaint_id')->references('id')-
            >on('complaints');

Now I have seeds :

  factory(Complaint::class, 10)->create()->each(function ($c) {

        $product = Product::inRandomOrder()->first();

        factory(ComplaintBill::class, 10)->create([
            'complaint_id' => $c->id,
            'item_id' => $product->id,
            'item_name' => $product->name,
            'item_numeric_index' => $product->numeric_index,
            'item_gross_price' => $product->sell_price_gross,
        ]);
     })'

I have problem/error like this:

 SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value 
 violates u  
 nique constraint "complaint_bills_pkey"                                      
 DETAIL:  Key (complaint_id)=(ea302ab8-67dc-3bed-afc8-4215a99f1f68) 
 already exists. 

When I comment primary in Complaint_bill ( column - complaint_id )then everything is ok. It looks like the problem is that I have primary key on uuid on Complaint_bill, which is foregin on Complaint->id. Why it is doing like this? I can't have primary on foregin when I have relation with two primary's?

wenus
  • 1,345
  • 6
  • 24
  • 51
  • 1
    Is relationship `complaint` to `complaint_bills` is `OneToMany` relationship? If yes, you shouldn't set `complaint_id` as `primary key` on `complaint_bills` table. Setting field as `primary key` means that there are no any duplicate. – Dharma Saputra Jan 26 '18 at 06:55
  • You are right- there is relation oneToOne and here was the problem. Thks for advice – wenus Jan 26 '18 at 06:58

2 Answers2

3

The reason you are seeing this issue is that a UUID is not an auto-incrementing value in the traditional sense that an INTEGER AUTO_INCREMENT is when defining a primary key. When using factory methods to insert a set of data, the value doesn't "go up by one" automatically on each insert.

You would need to initialise each model generated with a boot function to generate the UUID for yourself prior to storage. From the below article:

protected static function boot()
{
    parent::boot();

    static::creating(function ($model) {
        $model->{$model->getKeyName()} = Uuid::generate()->string;
    });
}

The below article explains it in a bit more detail.

https://medium.com/@steveazz/setting-up-uuids-in-laravel-5-552412db2088

Jason
  • 2,940
  • 2
  • 21
  • 34
  • 2
    This is a good solution but rather than depend on a third-party library as the article suggests, you can `use Illuminate\Support\Str;` and `Str::uuid()` to generate the UUID. HTH. – Ted Stresen-Reuter Jan 02 '21 at 11:35
1

Try this: \Ramsey\Uuid\Uuid::uuid4()->toString()

`DB::table('customers')->insert([    

    ['id' => \Ramsey\Uuid\Uuid::uuid4()->toString(), 'name' => 'Test', 'color' => '#aaa', 'created_at' => $now],  
]);`  
KD.S.T.
  • 573
  • 1
  • 5
  • 27