Saturday, October 11, 2025
HomeGolangHow one can create a row with GORM and affiliate the brand...

How one can 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 {
	Identify string
	Date time.Time
}

sort NewData struct {
	gorm.Mannequin
	Identify string
}

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

So then I’ve a scenario much like this:

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

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

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

How do I create the brand new newData row however on the identical 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 technique to relate newData to historical past which can be saved in two totally different tables.
db.Create(newData)

In accordance you describe the scenario, historical past and NewData must have the identical variety of data and I suppose that the Id fields are autonumeric so there a excessive probabilty we will get totally different id for a similar report quantity. You could possibly do :

  • Merge NewData and Historical past in a single desk (Undecided about this as a result of i have no idea all your online business necessities)
  • Add a Overseas Key in historical past to stablish a relationship with NewData. This manner by no means thoughts in the event that they did no have the identical id, purchase you might be optimistic taht you’ll be able to entry the suitable report in NewData from Historical past.

Simply my two cents…

NewData and Historical past are all the time created on the identical time, my drawback is associating the ID and the Date when creating a brand new one, each tables have the identical variety of ID’s, your second choice appears to me to be very near what I’m in search of, are you able to please present me some code?

I thibk you need to use this strategy :

  1. Add this annotation to you ID subject in historical past

    ID uint64 gorm:"primary_key;auto_increment;not_null"

You can even begin from the NewData struct, simply including the ID with above definition

  1. Situation a create operation for the historical past desk

    historical past := Historical past { Date : time.Now() }
    consequence := db.Create(&historical past)

  2. Now historical past has the newesst Id so assign an ID subject to your NewData struct and creates the report

    newData := NewData { Bame : “Your Identify”, historyID : historical past.ID }
    consequence := db.Create(&newData)

HTH,
Yamil

I noticed some GORM directions that did one thing much like this:

sort NewData struct {
	gorm.Mannequin
	Identify string
	Date time.Time gorm:"what I do right here?"
}

newData := &NewData{
	Identify: "someName",
    // Date is anticipated to be saved in one other desk different
    // than NewData however with the identical ID in NewData.
	Date: time.Now(),
}

// So, when executing this assertion NewData solely has the names column
// whereas Date is saved within the different desk (Historical past struct) that we've got related,
// the purpose is that I do not understand how that's accomplished.
db.Create(&newData)

You would wish so as to add the historyID subject to the NewData struct.
Examine this line

newData := NewData { Bame : “Your Identify”, HistoryID : historical past.ID }

sort NewData  struct {
    gorm.Mannequin
	Identify string
    HistoryID uint64
}      

However that’s 2 queries to the database, one to acquire historical past.ID and one other to create newData. I need (if potential) just one direct question.

Resolution:


sort NewData struct {
	gorm.Mannequin
	Identify string
    // use ID as international key
    Historical past Historical past `gorm:"foreignKey:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}

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

newData := &NewData{
    Identify: "Julia",
    Historical past: Historical past{
        Date: time.Now(),
    },
}

// Two rows might be created within the NewData and Historical past
// tables on the identical time with the identical ID.
db.Create(newData)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments