pages/es_ES/docs/constraints.md
GORM permite crear restricciones de base de datos con tag, las restricciones serán creadas al usar AutoMigrate or CreateTable con GORM
Crear restricción CHECK con el tag check
type UserIndex struct {
Name string `gorm:"check:name_checker,name <> 'jinzhu'"`
Name2 string `gorm:"check:name <> 'jinzhu'"`
Name3 string `gorm:"check:,name <> 'jinzhu'"`
}
GORM creará restricciones de llaves foráneas para asociaciones, usted puede deshabilitar esta característica durante la inicialización:
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
GORM permite configurar restricciones de llaves foráneas usando el tag constraint con las opciones OnDelete o OnUpdate, ver ejemplo:
type User struct {
gorm.Model
CompanyID int
Company Company `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
CreditCard CreditCard `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}
type CreditCard struct {
gorm.Model
Number string
UserID uint
}
type Company struct {
ID int
Name string
}