stephenafamo/bob

Support for mods to respect database constraints

Opened this issue · 1 comments

Lets say I have a schema:

// schema.sql

CREATE TABLE instructors (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR(60) NOT NULL,
    last_name VARCHAR(120) NOT NULL,
    full_name VARCHAR(255) GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED,
);

CREATE TABLE lessons (
    id SERIAL PRIMARY KEY,
    instructor_id INTEGER REFERENCES instructors(id),
    status VARCHAR(20),
);

When I try to create some data, it will fail because internally the data may get generated outside the max 20-character limit imposed by the lesson status field.

bobDB := bob.New[*sql.DB](db)
dbFactory := factory.New()

dbFactory.AddBaseInstructorMod(factory.InstructorMods.RandomizeAllColumns(&myFaker), factory.InstructorMods.UnsetID())
dbFactory.AddBaseLessonMod(factory.LessonMods.RandomizeAllColumns(&myFaker), factory.LessonMods.UnsetID(), factory.LessonMods.UnsetInstructorID())

_, err = dbFactory.NewInstructor(factory.InstructorMods.WithNewLessons(10)).CreateMany(ctx, bobDB, 10)

I can get around the lesson status by defining my own mods, its a bit tedious but certainly not a roadblock.

This will surely be useful. I'll tag it as an enhancement.