You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeUserstruct {
//Main columnsUserIduint`gorm:"primaryKey;autoIncrement" json:"userId"`UserFirstNamestring`gorm:"not null;default:NULL" json:"userFirstName"`UserLastNamestring`gorm:"not null;default:NULL" json:"userLastName"`UserUsernamestring`gorm:"not null;default:NULL;unique;<-:create" json:"userUsername,omitempty"`UserPasswordstring`gorm:"not null;default:NULL" json:"userPassword,omitempty"`UserRoleURole`gorm:"not null;default:OPERATOR" json:"userRole,omitempty"`UserAvatar []byte`gorm:"default:NULL;size:2097152" json:"userAvatar"`//Avatar only allow maximum size of 2MB=2097152Bytes//Extra fieldsAuthLeveluint`gorm:"-:all" json:"authLevel,omitempty"`
}
typeXDFirmwarestruct {
FWIdstring`gorm:"primaryKey;not null;unique" json:"fwId"`Versionstring`gorm:"not null;uniqueIndex:idx_fw_vers" json:"version"`MbedVersionstring`gorm:"not null;uniqueIndex:idx_fw_vers" json:"mbedVersion"`//Version string when do `ati` commandFreqstring`gorm:"not null;uniqueIndex:idx_fw_vers" json:"freq"`TargetXdotstring`gorm:"not null;uniqueIndex:idx_fw_vers" json:"targetXdot"`//Based on `controller.XDType`Checksumstring`gorm:"not null;unique" json:"checksum"`Path*string`gorm:"default:NULL;check:COALESCE(path, file_content) IS NOT NULL" json:"path"`//Path where the firmware locatedFilestruct {
FileContent*[]byte`gorm:"default:NULL;check:COALESCE(path, file_content) IS NOT NULL" json:"fileContent"`//Uploaded file. The firmware stored as bytesFileName*string`gorm:"check:file_content IS NULL OR (file_content IS NOT NULL AND file_name IS NOT NULL)" json:"fileName"`//The name of the uploaded fileMimeType*string`gorm:"check:file_content IS NULL OR (file_content IS NOT NULL AND mime_type IS NOT NULL)" json:"mimeType"`//Mime type for the uploaded file
} `gorm:"embedded" json:"file"`//CRUDCreateAt time.Time`gorm:"autoCreateTime:RFC3339" json:"createAt"`UpdateAt time.Time`gorm:"autoUpdateTime:RFC3339" json:"updateAt"`//Belongs ToCreatedBy*uint`gorm:"" json:"createdBy"`CreatedByInfoUser`gorm:"foreignKey:CreatedBy;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"createdByInfo"`//Extra fieldsPathFileChecksumstring`gorm:"-:all" json:"pathFileChecksum,omitempty"`UpFileChecksumstring`gorm:"-:all" json:"upFileChecksum,omitempty"`
}
typeItemstruct {
ItemIduint`gorm:"primaryKey;autoIncrement" json:"itemId"`ItemNumberstring`gorm:"not null;uniqueIndex:idx_item" json:"itemNumber"`XdotTypestring`gorm:"not null;uniqueIndex:idx_item" json:"xdotType"`//CRUDCreateAt time.Time`gorm:"autoCreateTime:RFC3339" json:"createAt"`UpdateAt time.Time`gorm:"autoUpdateTime:RFC3339" json:"updateAt"`//Belongs ToCreatedBy*uint`gorm:"" json:"createdBy"`CreatedByInfoUser`gorm:"foreignKey:CreatedBy;references:UserId;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"createdByInfo"`TestFirmware*string`gorm:"" json:"testFirmware"`TestFirmwareInfoXDFirmware`gorm:"foreignKey:TestFirmware;references:FWId;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"testFirmwareInfo"`WriteFirmware*string`gorm:"" json:"writeFirmware"`WriteFirmwareInfoXDFirmware`gorm:"foreignKey:WriteFirmware;references:FWId;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"writeFirmwareInfo"`
}
I used "github.com/glebarez/sqlite" for SQlite driver
ALL_MODELS= []any{&User{}, &XDFirmware{}, &Item{}}
dsn:=dbPath+"?_pragma=foreign_keys(1)"ifsDB, err:=gorm.Open(sqlite.Open(dsn), &gorm.Config{SkipDefaultTransaction: true, PrepareStmt: true}); err!=nil {
returnfmt.Errorf("failed to connect to server database, %v", err)
} else {
iferr:=sDB.AutoMigrate(ALL_MODELS...); err!=nil {
returnfmt.Errorf("failed auto migration for server database, %v", err)
}
}
The problem occurs when referencing Item.TestFirmware or Item.WriteFirmware with the parent table XDFirmware.FWId. According to the GORM documentation, DropTable should "ignore or delete foreign key constraints when dropping": https://gorm.io/docs/migration.html#Tables
I encounter the foreign key constraint failure when running AutoMigrate.
I know that I can use DisableForeignKeyConstraintWhenMigrating: true to bypasses the error, but it compromises the logic as it allows the deletion of parent rows, which should be restricted due to OnDelete:RESTRICT.
Not sure if this is expected behavior or a bug.
Thanks,
The text was updated successfully, but these errors were encountered:
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
GORM Playground Link
N/A
Description
My model:
I used
"github.com/glebarez/sqlite"
for SQlite driverThe problem occurs when referencing
Item.TestFirmware
orItem.WriteFirmware
with the parent tableXDFirmware.FWId
. According to the GORM documentation,DropTable
should "ignore or delete foreign key constraints when dropping": https://gorm.io/docs/migration.html#TablesI encounter the foreign key constraint failure when running
AutoMigrate
.I know that I can use
DisableForeignKeyConstraintWhenMigrating: true
to bypasses the error, but it compromises the logic as it allows the deletion of parent rows, which should be restricted due toOnDelete:RESTRICT
.Not sure if this is expected behavior or a bug.
Thanks,
The text was updated successfully, but these errors were encountered: