feat: many-to-one check, constrints check

This commit is contained in:
2025-03-17 13:13:35 +07:00
parent 4b5580b1c8
commit 60e0a64a24
6 changed files with 168 additions and 18 deletions

View File

@@ -4,25 +4,48 @@ package relations_check
type Library struct {
Id uint `gorm:"primaryKey"`
Books []*Book `gorm:"many2many:library_book;"`
Books []*Book `gorm:"many2many:library_book;constraint:OnDelete:CASCADE;"`
}
type Book struct {
Id uint `gorm:"primaryKey"`
Libraries []*Library `gorm:"many2many:library_book;"`
Libraries []*Library `gorm:"many2many:library_book;constraint:OnDelete:CASCADE;"`
}
type Employee struct {
Id uint `gorm:"primaryKey"`
Subordinates []*Employee `gorm:"many2many:employee_subordinates;"` // self-reference
Subordinates []*Employee `gorm:"many2many:employee_subordinates;constraint:OnDelete:CASCADE;"` // self-reference
}
type Publisher struct {
Id uint `gorm:"primaryKey"`
Writers []*Writer `gorm:"many2many:publisher_books;"`
Writers []*Writer `gorm:"many2many:publisher_books;constraint:OnDelete:CASCADE;"`
}
type Writer struct {
Id uint `gorm:"primaryKey"`
Publishers []Publisher `gorm:"many2many:publisher_books;"`
Publishers []Publisher `gorm:"many2many:publisher_books;constraint:OnDelete:CASCADE;"`
}
// One-to-many
type Comment struct {
Id uint `gorm:"primaryKey"`
CommentatorId uint
Commentator Commentator
}
type Commentator struct {
Id uint `gorm:"primaryKey"`
Comments []Comment `gorm:"foreignKey:CommentatorId;references:Id;constraint:OnDelete:CASCADE;"`
}
type Post struct {
Id uint `gorm:"primaryKey"`
Files []*File `gorm:"constraint:OnDelete:CASCADE;"`
}
type File struct {
Id uint `gorm:"primaryKey"`
PostId uint
Post Post
}

View File

@@ -2,20 +2,39 @@ package relations_check
type Student struct {
Id uint `gorm:"primaryKey"`
Courses []Course `gorm:"many2many:student_courses;"`
Courses []Course `gorm:"many2many:student_courses;constraint:OnDelete:CASCADE;"`
}
type Course struct {
Id uint `gorm:"primaryKey"`
Students []Course `gorm:"many2many:student_courses;"` // want "Invalid type `Course` in M2M relation \\(use \\[\\]\\*Student or self-reference\\)"
Students []Course `gorm:"many2many:student_courses;constraint:OnDelete:CASCADE"` // want "Invalid type `Course` in M2M relation \\(use \\[\\]\\*Student or self-reference\\)"
}
type Author struct {
Id uint `gorm:"primaryKey"`
Articles []Article `gorm:"many2many:author_articles;"`
Articles []Article `gorm:"many2many:author_articles;constraint:OnDelete:CASCADES;"`
}
type Article struct {
Id uint `gorm:"primaryKey"`
Authors Author `gorm:"many2many:author_articles;"` // want "M2M relation `author_articles` with bad type `Author` \\(should be a slice\\)"
Authors Author `gorm:"many2many:author_articles;constraint:OnDelete:CASCADE;"` // want "M2M relation `author_articles` with bad type `Author` \\(should be a slice\\)"
}
type Kuzbass struct {
Id uint `gorm:"primaryKey"`
Cities []City // want "Expected foreignKey `KuzbassId` in model `City` for 1:M relation with model `Kuzbass`"
}
type City struct {
Id uint `gorm:"primaryKey"`
Kuzbass Kuzbass
}
type Federation struct {
Lands []Land `gorm:"constraint:OnDelete:CASCADE;"` // want ""
}
type Land struct {
Id uint `gorm:"primaryKey"`
FederationId uint
}