diff --git a/store/migrations.go b/store/migrations.go index d440dd5cc0b304a98307dc4bc70d1ed7929aa3fd..55cc8e4dd132a168bb0c102dfdd6e1b4c7650a20 100644 --- a/store/migrations.go +++ b/store/migrations.go @@ -202,22 +202,7 @@ var ( ) func initialMigration(tx *gorm.DB) (err error) { - if err = tx.AutoMigrate(&Show{}, &File{}, &ImportLog{}); err != nil { - return err - } - - // replace the constraints created - if err = tx.Exec("ALTER TABLE files DROP CONSTRAINT fk_files_show").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE files ADD CONSTRAINT fk_files_show FOREIGN KEY (show_id) REFERENCES shows(id) ON UPDATE CASCADE ON DELETE CASCADE").Error; err != nil { - return err - } - - if err = tx.Exec("ALTER TABLE import_logs DROP CONSTRAINT fk_import_logs_file").Error; err != nil { - return err - } - return tx.Exec("ALTER TABLE import_logs ADD CONSTRAINT fk_import_logs_file FOREIGN KEY (file_id) REFERENCES files(id) ON UPDATE CASCADE ON DELETE CASCADE ").Error + return tx.AutoMigrate(&Show{}, &File{}, &ImportLog{}) } func (st *Store) initDBModel(cfg DBConfig) (err error) { diff --git a/store/types.go b/store/types.go index d3faf264c3228b0555870388b773a2248f27a5f1..31eb2129f0c0ab183c6496218af4cd98ee43279b 100644 --- a/store/types.go +++ b/store/types.go @@ -159,7 +159,7 @@ type File struct { CreatedAt time.Time `json:"created"` UpdatedAt time.Time `json:"updated"` ShowID uint64 `json:"showId" gorm:"not null;index"` - Show Show `json:"-" gorm:"associationForeignKey:ID"` + Show Show `json:"-" gorm:"associationForeignKey:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` Source FileSource `json:"source" gorm:"embedded;embeddedPrefix:source__"` Metadata FileMetadata `json:"metadata" gorm:"embedded;embeddedPrefix:metadata__"` Size uint64 `json:"size"` @@ -170,7 +170,7 @@ type File struct { type ImportLog struct { ID uint64 `gorm:"primaryKey"` - File File `gorm:"associationAutoUpdate:false;associationAutoCreate:false"` + File File `gorm:"associationAutoUpdate:false;associationAutoCreate:false;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` FileID uint64 `gorm:"not null;index;uniqueIndex:unique_import_log_step"` ImportStep string `gorm:"not null;index;uniqueIndex:unique_import_log_step"` Encoded []byte `gorm:"size:-1"`