Sunday, October 12, 2025
HomeGolangThe way to create a row with GORM and affiliate the brand...

The way to 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 state of affairs just 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 searching for a strategy to relate newData to historical past which might be saved in two totally different tables.
db.Create(newData)

In accordance you describe the state of affairs, historical past and NewData need to have the identical variety of information and I assume that the Id fields are autonumeric so there a excessive probabilty we will get totally different id for a similar report quantity. You would do :

  • Merge NewData and Historical past in a single desk (Undecided about this as a result of i have no idea all your enterprise necessities)
  • Add a Overseas Key in historical past to stablish a relationship with NewData. This fashion by no means thoughts in the event that they did no have the identical id, purchase you’re constructive taht you may entry the fitting 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 searching for, 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 may as well begin from the NewData struct, simply including the ID with above definition

  1. Concern 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 just like this:

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

newData := &NewData{
	Identify: "someName",
    // Date is predicted 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 want so as to add the historyID subject to the NewData struct.
Verify 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 attainable) just one direct question.

Resolution:


sort NewData struct {
	gorm.Mannequin
	Identify string
    // use ID as overseas 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 will probably 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