Skip to content

Instantly share code, notes, and snippets.

@jclosure
Created August 17, 2022 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclosure/21d151a76a36c8cf63ceed6dc002ecec to your computer and use it in GitHub Desktop.
Save jclosure/21d151a76a36c8cf63ceed6dc002ecec to your computer and use it in GitHub Desktop.
Cascade UPDATES and DELETES from parent to child table relations. Golang - Gorm
// HOW TO EXPRESSS CASCADING RELATIONS BETWEEN RELATED GORM MODELS
// if you update the server_id, it updates the server_id on sessions with old server_id
// if you delete the server, it deletes all sessions with same server_id
type Server struct {
Model
ServerID uuid.UUID `gorm:"type:uuid; primaryKey"`
# ...
Sessions []Session `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
type Session struct {
Model
SessionID uuid.UUID `gorm:"type:uuid; primaryKey"`
# ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment