I am working on a simple bird breeding application, because how else do we learn
So far its working like a charm. i can add/edit/delete birds, the problem arrises when i try to add fields to connect a bird to its parent. The problem is that we dont always have birds of which we have the parents too. But the best i can come up with is this struct:
type Bird struct {
gorm.Model
RingNo string `gorm:"not null"`
Species string `gorm:"not null"`
Color string `gorm:"not null"`
â– â– â– string `gorm:"not null"`
UserID uint `gorm:"not null"`
User User
FatherID *uint
Father *Bird `gorm:"foreignKey:FatherID;default:SET NULL"`
MotherID *uint
Mother *Bird `gorm:"foreignKey:MotherID;default:SET NULL"`
Notes []BirdNote `gorm:"foreignKey:BirdID"`
}
Basicly, what i’m trying to achieve is that FatherID and MotherID can be empty, but whatever i try, i always end up with a “violates key contraint” error