Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldBind sets a default value to *time.Time, cant use with gorm package #1364

Closed
dataflowjs opened this issue May 22, 2018 · 9 comments
Closed
Labels

Comments

@dataflowjs
Copy link

dataflowjs commented May 22, 2018

I have struct field DeletedAt *time.Time `sql:"index"`
After ctx.ShouldBind(&mystruct) the value of DeletedAt field is not nil, but must be, becouse gorm(sql) package cant auto generate NULL for that field.

The problem is there:
github.com\gin-gonic\gin\binding\form_mapping.go

if inputFieldName == "" {
	inputFieldName = typeField.Name

	// if "form" tag is nil, we inspect if the field is a struct or struct pointer.
	// this would not make sense for JSON parsing but it does for a form
	// since data is flatten
	if structFieldKind == reflect.Ptr {

                //!!!!!!!!!!!!!! here first we need to check if field is not time.Time (pointer to time)
                // if it is - continue
                //something like
                if structField.Type().Elem() == reflect.TypeOf(time.Time{}) {
                          continue
                 }
                //-------------------------------
		if !structField.Elem().IsValid() {

                        //!!!!!!!!!!!! here is the problem, why set default value?????????? I need nil!!!
			structField.Set(reflect.New(structField.Type().Elem()))
		}
		structField = structField.Elem()
		structFieldKind = structField.Kind()
	}
	if structFieldKind == reflect.Struct {
		err := mapForm(structField.Addr().Interface(), form)
		if err != nil {
			return err
		}
		continue
	}
}
@syssam
Copy link
Contributor

syssam commented May 23, 2018

Using form:"-" ignore DeletedAt field

@dataflowjs
Copy link
Author

dataflowjs commented May 23, 2018

@syssam yes it works, but bad that now i cant embed predefined struct gorm.Model and must rewrite every struct:

// gorm.Model
type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `gorm:"index"`
}

type MyStruct struct {
    gorm.Model
    //my fields
}

@syssam
Copy link
Contributor

syssam commented May 23, 2018

Surely, However you can custom your own gorm.Model
I haven't using gorm.Model directly, because I think the ID name should be different on every Model.
Therefore, I have not this problem.

@thinkerou
Copy link
Member

thinkerou commented May 23, 2018

ref #610 @syssam should use binding:"-", not form:"-", right?

@syssam
Copy link
Contributor

syssam commented May 23, 2018

binding:"-" is for validation
form:"-" is for form binding

@syssam
Copy link
Contributor

syssam commented May 23, 2018

the binding tag is base on https://github.com/gin-gonic/gin/blob/master/binding/default_validator.go
It register TagName: "binding", you can register another name, like validate

@yangjianhua
Copy link

@bsbak Hi, could you tell me how to solve this problem?

I tried, write a BaseModel just like gorm.Model, and set the form:"-", it still not ignore "created_at", "updated_at" and "deleted_at" field.

Hope you have solved the problem, thanks in advance.

@dataflowjs
Copy link
Author

@yangjianhua without seeing ur code cant say anything. form:"-" solved my problem. Try use db.Omit("created_at, updated_at, deleted_at").Create(...)

@yangjianhua
Copy link

@bsbak Thanks you, but omit only work on update method, right?

It seems not work on create

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants