英文:
How can I change the column name of a resulting table from many to many in gorm
问题
我已经使用TypeORM玩了很多Nestjs,现在我正在学习使用Golang和Gorm。我正在用Nestjs为Go重建一个系统,并且在使用多对多关系时遇到了问题。由于我正在使用已经在生产和填充的数据库,所以我需要将结构模型与其他项目完全相同,以避免出现问题。
我有两个表以多种方式相关联,它们是playlists和genres,在生产数据库中它们会生成名为playlists_genres的表,具有以下字段:
请注意,字段类似于playlistsId和genresId。
请注意,生成的字段与我需要的不同,我需要为生产数据库生成标识符,即playlistsId和genresId。
我的关系代码如下:
type Playlist struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement:true"`
Status bool `json:"status" gorm:"not null;default:false"`
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at;autoCreateTime:milli;type:TIMESTAMP without time zone;default:now();not null"`
UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at;autoUpdateTime:milli;type:TIMESTAMP without time zone;default:now();not null"`
Genres []Genre `json:"genres" gorm:"many2many:playlists_genres"`
}
和
type Genre struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement:true"`
Name string `json:"name" gorm:"not null;type:varchar(255)"`
Playlists []Playlist `json:"playlists" gorm:"many2many:playlists_genres"`
}
我已经查了很多,没有找到除了手动创建一个具有我需要的列名的中间表并从一对多关联它之外的方法。
你能帮我解决这个问题吗?是否可以在不手动创建表的情况下实现这一点?谢谢!
英文:
I already played a lot with Nestjs using TypeORM and now I'm learning to work with Golang and Gorm. I'm rebuilding a system in Nestjs for Go and I'm experiencing a problem when using many2many relations. As I am working with a database already in production and populated, I need to model the struct`s identically to the other project so that there are no problems.
I have two tables that relate in a multiple way, they are playlists and genres and in the database in production they result in the playlists_genres table with the following fields:
Note that the fields are like playlistsId and genresId
Already in the project in Golang the result of the m2m of the tables are resulting in:
Note that the generated fields are different from what I need, I need to generate identifiers for the database in production, that is playlistsId and genresId.
My relationship code looks like this:
type Playlist struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement:true"`
Status bool `json:"status" gorm:"not null;default:false"`
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at;autoCreateTime:milli;type:TIMESTAMP without time zone;default:now();not null"`
UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at;autoUpdateTime:milli;type:TIMESTAMP without time zone;default:now();not null"`
Genres []Genre `json:"genres" gorm:"many2many:playlists_genres"`
}
and
type Genre struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement:true"`
Name string `json:"name" gorm:"not null;type:varchar(255)"`
Playlists []Playlist `json:"playlists" gorm:"many2many:playlists_genres"`
}
I've looked a lot and I haven't found a way to change the name of these columns other than manually creating an intermediate table with the columns with the names I need and relating it from one to many.
Could you help me with this? Is it possible to do this without creating a table manually? Thanks!
答案1
得分: 1
很遗憾,我认为实现你的目标的唯一方法是创建一个名为PlaylistGenre
的结构体,并在此处覆盖列的名称。
使用Gorm约定,您只能对数据库中创建的ForeignKey约束进行操作。无法自动覆盖联接表的实际列名称。
在您的gorm
注释中,您可以使用这些属性来处理外键:foreignKey
、references
、joinForeignKey
和joinReferences
。
然而,在这里使用column
是不可能的。
如果需要使用这种方法的示例,请告诉我!
英文:
Unfortunately, I think the only way to achieve your goal is to create a struct called PlaylistGenre
and override the names of the columns here.
With Gorm conventions, you can only act on the ForeignKey constraints created in your database. No chance to override the actual column names of the join table automatically created.
In your gorm
annotation you can use these properties to deal with the foreign keys: foreignKey
, references
, joinForeignKey
, and joinReferences
.
However, you need to use the column
, which is impossible here.
Let me know if you need an example with this approach!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论