Skip to content

$uniqueFaker for x-indexes #115

@siggi-k

Description

@siggi-k

Faker: Use $uniqueFaker for attributes listed in x-indexes unique constraints

Problem

When an attribute appears in an x-indexes entry that defines a unique constraint
(e.g. unique:table,column), the faker stub generator uses the plain $faker instance
for that attribute. This causes duplicate values during fake data generation, which
violates the unique constraint and leads to database errors at runtime.

Expected Behavior

If an attribute is referenced in any x-indexes entry that starts with unique:, the
generator should automatically use $uniqueFaker instead of $faker in the generated
faker stub — unless the spec already provides an explicit x-faker value that already
uses $uniqueFaker.

Example

x-indexes:
  - 'unique:leads,creditor_number'

properties:
  creditor_number:
    type: integer
    minimum: 10000
    maximum: 99999

Expected generated faker stub:

$model->creditor_number = $uniqueFaker->numberBetween(10000, 99999);

Actual (broken) generated faker stub:

$model->creditor_number = $faker->numberBetween(10000, 99999);

Note on composite unique indexes

The issue also applies to composite unique indexes (e.g. unique:lead_mails,lead_id,mail).
For composite indexes, at least the varying part of the key (typically the second or third
column) should use $uniqueFaker — or the full combination should be guaranteed unique.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions