Describe the feature

this feature comes from the django orm,when i want made a model that have col of several choices,i want give a uint8 ,it will hard in gorm. i wonder if it is possible to give a gorm tag to do this.

type User struct{
   ...
   Sex string  `gorm:"type:int;choice:male,femle,unknown" `
}

Motivation

in many projects ,even though we have one to many or many to one relations,but often, we don't want the choices become a table in databases, because it only have less to 5 choices.
and this will be useful.

Related Issues

Comment From: black-06

You mean, you want to store a small number of enums in uint8 / tinyint?

Comment From: jinzhu

GORM won't include data validator, checkout https://github.com/qor/validations for how to create one by yourself.

Comment From: blight19

You mean, you want to store a small number of enums in uint8 / tinyint?

just like this :https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.Field.choices

Comment From: blight19

You mean, you want to store a small number of enums in uint8 / tinyint? yes,the data will store in db as int,and when use in golang ,it reflect to string,i think it is very useful to save storage and improve proformance.

Comment From: black-06

You mean, you want to store a small number of enums in uint8 / tinyint?

yes,the data will store in db as int,and when use in golang ,it reflect to string,i think it is very useful to save storage and improve proformance.

This should not be part of ORM. What you need is probably a golang enum. For example:

type BodyPart int

const (
    Head BodyPart = iota // Head = 0
    Shoulder             // Shoulder = 1
    Knee                 // Knee = 2
    Toe                  // Toe = 3
)

func (bp BodyPart) String() string {
    return []string{"Head", "Shoulder", "Knee", "Toe"}[bp]
}

Comment From: mahesh-riddles

You mean, you want to store a small number of enums in uint8 / tinyint?

yes,the data will store in db as int,and when use in golang ,it reflect to string,i think it is very useful to save storage and improve proformance.

This should not be part of ORM. What you need is probably a golang enum. For example:

```go type BodyPart int

const ( Head BodyPart = iota // Head = 0 Shoulder // Shoulder = 1 Knee // Knee = 2 Toe // Toe = 3 )

func (bp BodyPart) String() string { return []string{"Head", "Shoulder", "Knee", "Toe"}[bp] } ```

I tried this.. Why those fields, which I wanted to be int (enum) are now big int?