Skip to content

Gorm Struct Tags? #642

Discussion options

You must be logged in to vote

gorm struct tags are needed for automigrations to run. however, since we moved off gorm automigrate, here are my observations where struct tags are necessary:

when there is a many to many (see)

when there is a has many, with a differently named foreign key ID

has many example from gorm

// User has many CreditCards, UserID is the foreign key
type User struct {
  	gorm.Model
  	CreditCards []CreditCard
}

type CreditCard struct {
  	gorm.Model
  	Number string
  	UserID uint
}

explicit has many foreign key required (forgive the contrived example)

// User has many CreditCards, CustomerID is the foreign key
type User struct {
  	gorm.Model
  	CreditCards []CreditCard `gorm:"foreignKey:Custome…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by garrettladley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant