英文:
proplem gorm many to many get list
问题
问题:
错误是在结构honardari/api/models.CategoryModel的字段PlayList中找到无效字段:为关系定义一个有效的外键或实现Valuer/Scanner接口。
请帮助我
包 models:
type PlayListModel struct
{
Id int `gorm:"column:id" json:"id"`
Published string `gorm:"column:published" json:"published"`
Type_PlayLIST1 string `gorm:"column:type_playlist1" json:"type_playlist1"`
Type_PlayLIST2 string `gorm:"column:type_playlist2" json:"type_playlist2"`
Type_PlayLIST3 string `gorm:"column:type_playlist3" json:"type_playlist3"`
Type_PlayLIST4 string `gorm:"column:type_playlist4" json:"type_playlist4"`
Price int `gorm:"column:price" json:"price"`
Price_Orginal int `gorm:"column:price_orginal" json:"price_orginal"`
Main_img string `gorm:"column:main_img" json:"main_img"`
UserId int `gorm:"column:user_id"`
LIkesCount string `gorm:"column:likes_count" json:"likes_count"`
ViewsCount string `gorm:"column:views_count" json:"views_count"`
Content string `gorm:"column:content" json:"content"`
Title string `gorm:"column:title" json:"title"`
Categories []CategoryModel `gorm:"many2many:app_products_playlist_category"`
}
type CategoryModel struct {
ID string `gorm:"column:id" json:"id"`
Name string `gorm:"column:name" json:"name"`
Img string `gorm:"column:img" json:"img"`
Slug string `gorm:"column:slug" json:"slug"`
Row string `gorm:"column:row" json:"row"`
ParenID string `gorm:"column:parent_id" json:"parent_id"`
PlayList []PlayListModel `gorm:"many2many:app_products_playlist_category"`
}
type PlayListCategoryModel struct {
PlayListID int `gorm:"primary_key;column:playlist_id" json:"playlist_id"`
CategoryID int `gorm:"primary_key;column:category_id" json:"category_id"`
PlayListModel
CategoryModel
}
func (PlayListCategoryModel) TableName() string {
return "app_products_playlist_category"
}
func (category *CategoryModel) TableName() string {
return "app_category_category"
}
func (playlist *PlayListModel) TableName() string {
return "app_products_playlist"
}
英文:
proplem:
error is invalid field found for struct honardari/api/models.CategoryModel's field
PlayList: define a valid foreign key for relations or implement the Valuer/Scanner
interface
pleas help me
packages models :
type PlayListModel struct
{
Id int `gorm:"column:id" json:"id"`
Published string `gorm:"column:published" json:"published"`
Type_PlayLIST1 string `gorm:"column:type_playlist1" json:"type_playlist1"`
Type_PlayLIST2 string `gorm:"column:type_playlist2" json:"type_playlist2"`
Type_PlayLIST3 string `gorm:"column:type_playlist3" json:"type_playlist3"`
Type_PlayLIST4 string `gorm:"column:type_playlist4" json:"type_playlist4"`
Price int `gorm:"column:price" json:"price"`
Price_Orginal int `gorm:"column:price_orginal" json:"price_orginal"`
Main_img string `gorm:"column:main_img" json:"main_img"`
UserId int `gorm:"column:user_id"`
LIkesCount string `gorm:"column:likes_count" json:"likes_count"`
ViewsCount string `gorm:"column:views_count" json:"views_count"`
Content string `gorm:"column:content" json:"content"`
Title string `gorm:"column:title" json:"title"`
Categories []CategoryModel `gorm:"many2many:app_products_playlist_category"`
}
type CategoryModel struct {
ID string `grom:column:"id" json:"id"`
Name string `grom:column:"name" json:"name"`
Img string `grom:column:"img" json:"img"`
Slug string `grom:column:"slug" json:"slug"`
Row string `grom:column:"row" json:"row"`
ParenID string `grom:column:"parent_id" json:"parent_id"`
PlayList []PlayListModel `gorm:many2many:app_products_playlist_category`
}
type PlayListCategoryModel struct {
PlayListID int `gorm:"primary_key;column:playlist_id" json:"playlist_id"`
CategoryID int `gorm:"primary_key;column:category_id" json:"category_id"`
PlayListModel
CategoryModel
}
func (PlayListCategoryModel) TableName() string {
return "app_products_playlist_category"
}
func (category *CategoryModel) TableName() string {
return "app_category_category"
}
func (playlist *PlayListModel) TableName() string {
return "app_products_playlist"
}
答案1
得分: 1
问题只是一个拼写错误,在这段代码中:
PlayList []PlayListModel `gorm:many2many:app_products_playlist_category`
你需要使用双引号,所以改成这样:
PlayList []PlayListModel `gorm:"many2many:app_products_playlist_category"`
只是建议,使用约定而不是自己配置所有内容,所以最好是这样的:
type PlayListModel struct
{
gorm.Model
...
}
type CategoryModel struct {
gorm.Model
...
}
看,我正在使用gorm.model
,自动地我有ID和其他字段。
英文:
The problem is just a typo, in this code:
PlayList []PlayListModel `gorm:many2many:app_products_playlist_category`
You need double quotes, so change to this:
PlayList []PlayListModel `gorm:"many2many:app_products_playlist_category"`
Just advice, use convention instead of configuring everything by yourself, so better should be something like that:
type PlayListModel struct
{
gorm.Model
...
type CategoryModel struct {
gorm.Model
...
Look, I'm using gorm.model
and automatically I have ID and others fields.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论