Sunday, October 12, 2025
HomeGolangcreate a row with GORM and affiliate the brand new ID created...

create a row with GORM and affiliate the brand new ID created within the row of one other desk? – Getting Assist


I’ve the next tables:

sort OldData struct {
	Title string
	Date time.Time
}

sort NewData struct {
	gorm.Mannequin
	Title string
}

sort Historical past struct {
	ID uint
	Date time.Time
}

So then I’ve a state of affairs just like this:

oldData := &OldData{
	Title: "Julia",
	Date: time.Now(),
}

newData := &NewData{
	Title: oldData.Title,
}

historical past := &Historical past{
	Date: oldData.Date,
}

How do I create the brand new newData row however on the similar time have historical past.ID be the identical ID as newData.ID? How do I make a relationship in order that if I delete the newData.ID row the historical past.ID row can be deleted from the historys desk?

I’m in search of a solution to relate newData to historical past which can be saved in two completely different tables.
db.Create(newData)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments