go.uuid icon indicating copy to clipboard operation
go.uuid copied to clipboard

uuid: cannot convert uuid.UUID to UUID

Open jarifibrahim opened this issue 6 years ago • 3 comments

I have the following struct

type Identity struct {
	gormsupport.Lifecycle
	// Link to User
	UserID uuid.NullUUID `sql:"type:uuid"`
	User   User
}

and

identity := account.Identity{
		User:         user,
		UserID:       uuid.NullUUID{UUID: uuid.NewV4(), Valid: true},
	}
err := s.identityRepo.Create(context.Background(), &identity)

and this is the Create method being called

// Create creates a new record.
func (m *GormIdentityRepository) Create(ctx context.Context, model *Identity) error {
	defer goa.MeasureSince([]string{"goa", "db", "identity", "create"}, time.Now())
	if model.ID == uuid.Nil {
		model.ID = uuid.NewV4()
	}
	err := m.db.Create(model).Error
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"identity_id": model.ID,
			"err":         err,
		}, "unable to create the identity")
		return errs.WithStack(err)
	}

The line m.db.Create(model).Error is failing with cannot convert uuid.UUID to UUID

jarifibrahim avatar May 29 '18 14:05 jarifibrahim

Any update on this issue?

tamasd avatar May 30 '20 20:05 tamasd

How to solve this issue for now? I have seen this erros

type struct Me {
  ID uuid.UUID
}

...


if Me.ID != nil {
  // cannot convert untyped nil to uuid.UUID
} 

Happen even with uuid.NullUUID

mandaputtra avatar Sep 24 '20 15:09 mandaputtra

I solve it using pointer so it could be a nil

mandaputtra avatar Oct 01 '20 13:10 mandaputtra