Closed zenire closed 1 year ago
Hello @zenire, can you share the code of the models.Object{}
struct? I tested with the below test in the struct_test.go
file and it worked as expected.
func TestStructUUIDInSequence(t *testing.T) {
var st struct {
UUID string `fake`
}
fake := New()
before := ""
for i := 0; i < 100; i++ {
fake.Struct().Fill(&st)
after := st.UUID
NotExpect(t, true, after == "")
Expect(t, false, before == after)
before = after
}
}
Regarding the usage of the google/uuid
library. The purpose of this library is to be self-sufficient and do not depend on any external code.
Thank you for looking into this @jaswdr
package models
import (
"time"
"github.com/jaswdr/faker"
"gorm.io/gorm"
)
// Object is the model for the Object
type Object struct {
ID uuid.UUID `gorm:"primary_key;unique;type:uuid;column:id;default:uuid_generate_v4()"`
Name string
Description string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
// Factory is used to generate fake data
func (m *Object) Factory() {
fake := faker.New()
fake.Struct().Fill(m)
// We don't want to delete the model
m.DeletedAt = gorm.DeletedAt{}
}
I understand the purpose of the library, and I think it's a good idea to be self-sufficient.
I tested the below code with the latest version and it worked as expected, please use the latest version and try again.
func TestFactory(t *testing.T) {
var st struct {
UUID string `gorm:"primary_key;unique;type:uuid;column:id;default:uuid_generate_v4()"`
}
fake := New()
fake.Struct().Fill(&st)
Expect(t, false, st.UUID == "")
}
Unfortunately I'm experiencing the same issue with commit 3bd11b19b027f3c2c15e620a79d15c5013ebaa6d
. Will test again next week and share some more details.
I opened a PR after mildly changing the way the variant bits were set. It should fix the problem now I think.
Fix was merged, I'm closing this for now, if you still face the issue please reopen.
Describe the bug When I generate UUID's rapidly (for a test for example) the UUIDs are not random and unique.
To Reproduce
Expected behavior Generate a random and unique UUID each time.
Desktop (please complete the following information):
go version
output: go version go1.20.1 darwin/arm64Additional context My suggestion is to generate UUID's using a library like "github.com/google/uuid"