Created
August 17, 2022 05:24
-
-
Save jclosure/21d151a76a36c8cf63ceed6dc002ecec to your computer and use it in GitHub Desktop.
Cascade UPDATES and DELETES from parent to child table relations. Golang - Gorm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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