Gorm: FirstOrCreate with has many foreign key id mapping?
Gorm: FirstOrCreate with has many foreign key id mapping?
I am trying to do following:
Each execution is different, so collection -> executions is a one-to-many mapping.
type Collection struct
ID int
CollectionName string gorm:"primary_key"
CommitHash string gorm:"primary_key"
Executions Execution
type Execution struct
TimeStamp time.Time
SubDomain string
CollectionID int
And Here is main
db.AutoMigrate(&Collection, &Execution)
execution := Execution
TimeStamp: time.Now(),
SubDomain: "dev"
db.Create(&execution)
collection := Collection
CollectionName: "Testing.json",
CommitHash: "123456",
Executions: Executionexecution
db.FirstOrCreate(&collection)
Current result for collection is fine -- it's not creating a new record if duplicated. But the execution record is generating all 0 for collection_id column.
After executing three times, the result will be like this:
id time_stamp subdomain collection_id
1 2018-07-31 00:18:42.341261+00 dev 1
2 2018-07-31 00:19:03.779787+00 dev 0
3 2018-07-31 00:19:15.626205+00 dev 0
while there's only one 1 Testing.json 123456
record in the collection table.
1 Testing.json 123456
How can I mapping the collection_id back correctly?
Thanks!!!
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment