Hi guys. I'm having an issue using an interface to insert data using GORM:
func MyFunc (...){
...
entity, errEntity := repositories.Create(&tenant)
...
}
func Create(entity interface{}) (interface{}, error) {
db, errDb := helpers.DbInit()
if errDb != nil {
return nil, db.Error
}
h := helpers.NewDbConnection(db)
result := h.DB.Create(&entity)
if result.Error != nil {
return nil, result.Error
}
return entity, nil
}
Error:
INSERT INTO "tenant" ("cognito_sub","email","name","parent_id","tenant_type","is_active","created_by","created_at","updated_by","updated_at") VALUES RETURNING "id"
{"message":"Error executing database operation","details":"unsupported data"}
looks like values are missing on the SQL statement. The way I found to make it work is to pass the type, like this:
func MyFunc (...){
...
entity, errEntity := repositories.Create[models.Tenant](&tenant)
...
}
func Create[T any](entity interface{}) (interface{}, error) {
var returnEntity T
if entity, ok := requestedEntity.(T); ok {
returnEntity = entity
}
db, errDb := helpers.DbInit()
if errDb != nil {
return nil, db.Error
}
h := helpers.NewDbConnection(db)
result := h.DB.Create(&returnEntity)
if result.Error != nil {
return nil, result.Error
}
return returnEntity, nil
}
I was wondering if I could solve it without having to pass the type