Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Seeding multiple unique columns with Faker

Introduction

What up folks, I got a question about model factories and multiple unique columns:

Background

I have a model named Image. This model has language support stored in a separate model, ImageText. ImageText has an image_id column, a language column and a text column.

ImageText has a constraint in MySQL that the combination image_id and language has to be unique.

class CreateImageTextsTable extends Migration
{

    public function up()
    {
        Schema::create('image_texts', function ($table) {

            ...

            $table->unique(['image_id', 'language']);

            ...

        });
    }

    ...

Now, I want each Image to have several ImageText models after seeding is done. This is easy with model factories and this seeder:

factory(App\Models\Image::class, 100)->create()->each(function ($image) {
    $max = rand(0, 10);
    for ($i = 0; $i < $max; $i++) {
        $image->imageTexts()->save(factory(App\Models\ImageText::class)->create());
    }
});

Problem

However, when seeding this using model factories and faker, you are often left with this message:

[PDOException]                                                                                                                 
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '76-gn' for key 'image_texts_image_id_language_unique'

This is because at some point, inside that for loop, the faker will random the same languageCode twice for an image, breaking the unique constraint for ['image_id', 'language'].

You can update your ImageTextFactory to say this:

$factory->define(App\Models\ImageText::class, function (Faker\Generator $faker) {

    return [
        'language' => $faker->unique()->languageCode,
        'title' => $faker->word,
        'text' => $faker->text,
    ];
});

But then, you instead get the problem that the faker will run out of languageCodes after enough imageTexts have been created.

Current solution

This is currently solved by having two different factories for the ImageText, where one resets the unique counter for languageCodes and the seeder calls the factory which resets te unique counter before entering the for loop to create further ImageTexts. But this is code duplication, and there should be a better way to solve this.

The question

Is there a way to send the model you are saving on into the factory? If so, I could have a check inside the factory to see if the current Image has any ImageTexts attached already and if it doesn't, reset the unique counter for languageCodes. My goal would be something like this:

$factory->define(App\Models\ImageText::class, function (Faker\Generator $faker) {

    $firstImageText = empty($image->imageTexts());

    return [
        'language' => $faker->unique($firstImageText)->languageCode,
        'title' => $faker->word,
        'text' => $faker->text,
    ];
});

Which of course currently gives:

[ErrorException]           
Undefined variable: image

Is it possible to achieve this somehow?

like image 508
Rkey Avatar asked Apr 04 '17 09:04

Rkey


3 Answers

I built on Rkey's answer to suit my needs:

problem
I have two integer fields that together should be unique, these are product_id and branch_id.

solution
Heres's my approach:

  1. Get the total number of products and branches. Since the ids are generated from 1, the ids shall range from 1 to the-total-count-of-items-in-the-table(s).
  2. Create all possible unique values that can be created from product_id and branch_id by creating a string separated by a character, in this case -
  3. Generate unique random values from this set using the randomElements function.
  4. Split the random element back to product_id and branch_id
    $branch_count = Branch::all()->count();
    $product_count = Product::all()->count();

    $branch_products = [];
    for ($i = 1; $i <= $branch_count; $i++) {
      for ($j = 1; $j <= $product_count; $j++) {
        array_push($branch_products, $i . "-" . $j);
      }
    }

    $branch_and_product = $this->faker->unique->randomElement($branch_products);

    $branch_and_product = explode('-', $branch_and_product);
    $branch_id = $branch_and_product[0];
    $product_id = $branch_and_product[1];

    return [
      // other fields
      // ...
      "branch_id" =>  $branch_id,
      "product_id" => $product_id
    ];
like image 80
Timothy Karani Avatar answered Oct 24 '22 10:10

Timothy Karani


I solved it

I searched a lot for a solution to this problem and found that many others also experienced it. If you only need one element on the other end of your relation, it's very straight forward.

The addition of the "multi column unique restriction" is what made this complicated. The only solution I found was "Forget the MySQL restriction and just surround the factory creation with a try-catch for PDO-exceptions". This felt like a bad solution since other PDOExceptions would also get caught, and it just didn't feel "right".

Solution

To make this work I divided the seeders to ImageTableSeeder and ImageTextTableSeeder, and they are both very straight forward. Their run commands both look like this:

public function run()
{
    factory(App\Models\ImageText::class, 100)->create();
}

The magic happens inside the ImageTextFactory:

$factory->define(App\Models\ImageText::class, function (Faker\Generator $faker) {

    // Pick an image to attach to
    $image = App\Models\Image::inRandomOrder()->first();
    $image instanceof App\Models\Image ? $imageId = $image->id : $imageId = null;

    // Generate unique imageId-languageCode combination
    $imageIdAndLanguageCode = $faker->unique()->regexify("/^$imageId-[a-z]{2}");
    $languageCode = explode('-', $imageIdAndLanguageCode)[1];

    return [
        'image_id' => $imageId,
        'language' => $languageCode,
        'title' => $faker->word,
        'text' => $faker->text,
    ];
});

This is it:

$imageIdAndLanguageCode = $faker->unique()->regexify("/^$imageId-[a-z]{2}");

We use the imageId in a regexify-expression and add whatever is also included in our unique combination, separated in this case with a '-' character. This will generate results like "841-en", "58-bz", "96-xx" etc. where the imageId is always a real image in our database, or null.

Since we stick the unique tag to the language code together with the imageId, we know that the combination of the image_id and the languageCode will be unique. This is exactly what we need!

Now we can simply extract the created language code, or whatever other unique field we wanted to generate, with:

$languageCode = explode('-', $imageIdAndLanguageCode)[1];

This approach has the following advantages:

  • No need to catch exceptions
  • Factories and Seeders can be separated for readability
  • Code is compact

The disadvantage here is that you can only generate key combinations where one of the keys can be expressed as regex. As long as that's possible, this seems like a good approach to solving this problem.

like image 10
Rkey Avatar answered Oct 24 '22 12:10

Rkey


Your solution only works for things that can be regexified as a combination. There are many use cases where a combination of multiple separate Faker generated numbers/strings/other objects need to be unique and cannot be regexified.

For such cases you can do something like so:

$factory->define(App\Models\YourModel::class, function (Faker\Generator $faker) {
    static $combos;
    $combos = $combos ?: [];
    $faker1 = $faker->something();
    while($faker2 = $faker->somethingElse() && in_array([$faker1, $faker2], $combos) {}
    $combos[] = [$faker1, $faker2];
    return ['field1' => $faker1, 'field2' => $faker2];
});

For your specific question / use case, here's a solution on the same lines:

$factory->define(App\Models\ImageText::class, function (Faker\Generator $faker) {

    static $combos;
    $combos = $combos ?: [];

    // Pick an image to attach to
    $image = App\Models\Image::inRandomOrder()->first();
    $image instanceof App\Models\Image ? $imageId = $image->id : $imageId = null;

    // Generate unique imageId-languageCode combination
    while($languageCode = $faker->languageCode && in_array([$imageId, $languageCode], $combos) {}
    $combos[] = [$imageId, $languageCode];

    return [
        'image_id' => $imageId,
        'language' => $languageCode,
        'title' => $faker->word,
        'text' => $faker->text,
    ];
});
like image 1
Paras Avatar answered Oct 24 '22 11:10

Paras