I'm trying to save a new object to my database. For the sake of this example, I will use the following:
type TimeStamps struct {
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
... a few other fields
}
type OrderBase struct {
TimeStamps
ID uint `json:"id,omitempty" gorm:"primarykey"`
... a few other fields
}
type Order struct {
OrderBase
Reference string `json:"reference,omitempty"`
}
newOrder := Order{...filled order...}
db.Save(&newOrder)
What I'm expecting to happen, and what did happen when the ID field lived directly within the Order struct, is after the save I can call newOrder.ID and have the latest ID value (no longer the default 0).
What I'm seeing now is, the order is saved to the DB however when I call newObject.ID, it's still 0.
From what I can tell after doing a lot of research, it looks like there is no way to get around this and the only way to get the result I want is to specify ID, createdAt and updatedAt in each of my structs individually. Is this the case?
I'm happy to use small workarounds like putting code in BeforeSave or AfterSave (which actually shows the updated value!).
Thanks
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: byroncoetsee
That's a well structured question and was marked as reproduce steps missing (they're there) and stale because I haven't linked to a playground, which requires all kinds of setups and new programs like Docker, which I don't use. Just to ask a question which is very easy to understand...
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: black-06
https://github.com/go-gorm/playground/pull/600
But It works.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨