How do I create the new newData row but at the same time have history.ID be the same ID as newData.ID? How do I make a relationship so that if I delete the newData.ID row the history.ID row is also deleted from the historys table?
I’m looking for a way to relate newData to history that are stored in two different tables. db.Create(newData)
According you describe the situation, history and NewData have to have the same number of records and I guess that the Id fields are autonumeric so there a high probabilty we can get different id for the same record number. You could do :
Merge NewData and History in one table (Not sure about this because i do not know all your business requirements)
Add a Foreign Key in history to stablish a relationship with NewData. This way never mind if they did no have the same id, buy you are positive taht you can access the right record in NewData from History.
NewData and History are always created at the same time, my problem is associating the ID and the Date when creating a new one, both tables have the same number of ID’s, your second option seems to me to be very close to what I am looking for, can you please show me some code?
I saw some GORM instructions that did something similar to this:
type NewData struct {
gorm.Model
Name string
Date time.Time gorm:"what I do here?"
}
newData := &NewData{
Name: "someName",
// Date is expected to be stored in another table other
// than NewData but with the same ID in NewData.
Date: time.Now(),
}
// So, when executing this statement NewData only has the names column
// while Date is stored in the other table (History struct) that we have associated,
// the point is that I don't know how that is done.
db.Create(&newData)
type NewData struct {
gorm.Model
Name string
// use ID as foreign key
History History `gorm:"foreignKey:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}
type History struct {
ID uint
Date time.Time
}
newData := &NewData{
Name: "Julia",
History: History{
Date: time.Now(),
},
}
// Two rows will be created in the NewData and History
// tables at the same time with the same ID.
db.Create(newData)