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

how use json null set sql time field to null? #70

Open
ystyle opened this issue Dec 9, 2022 · 3 comments
Open

how use json null set sql time field to null? #70

ystyle opened this issue Dec 9, 2022 · 3 comments

Comments

@ystyle
Copy link

ystyle commented Dec 9, 2022

  1. defind web api dto struct
type Dto struct {
   Start null.Time
}
  1. send this json to web api
{ 
  "Start": null
}
  1. service: use gorm update entry
gorm.Model(&MyTable).Where("id = 1").Update(&dto)
  1. Hope to generate sql
update my_table set start = null  where id = 1

The start field is now ignored when used in this way

@guregu
Copy link
Owner

guregu commented Dec 10, 2022

Sounds like a GORM issue to me. I don't use it and I can't provide support for it.
I would suggest that after you unmarshal the JSON input, check whether null.Time is properly set to null or not. If it looks OK, then it's almost certainly a GORM issue.

@ystyle
Copy link
Author

ystyle commented Mar 15, 2023

The result is the same whether the time type is passed or not, how to set a zero value for the time
image

@oogali
Copy link

oogali commented Jan 27, 2024

I feel a better option would be to use the Golang stdlib data structures for null SQL values. Those will generally be supported by all Golang ORMs, including GORM as their documentation on model declaration specifically uses them (https://gorm.io/docs/models.html).

The relevant type here would be: sql.NullTime (https://pkg.go.dev/database/sql#NullTime)

Unfortunately, you will need to have logic to copy the data from the source type into the destination type. However, you would have incurred that "extra" step no matter what.

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

No branches or pull requests

3 participants