Home  >  Article  >  Backend Development  >  How to change result table column name from "many" to "many" in gorm

How to change result table column name from "many" to "many" in gorm

PHPz
PHPzforward
2024-02-10 13:45:081042browse

如何在 gorm 中将结果表的列名从“多”更改为“多”

When using gorm for database operations, sometimes we may need to change the column name of the result table from "many" to "many". Doing this can make our code clearer and easier to read and improve the maintainability of the code. So, how to implement this function in gorm? In this article, PHP editor Strawberry will share with you a simple and effective method to help you easily modify the column names of the results table.

Question content

I have played a lot with nestjs using typeorm and now I am learning to use golang and gorm. I'm rebuilding a system in nestjs for go and I'm having trouble using a many-to-many relationship. Since I'm working with a database that's already in production and populated, I need to model the structure the same as other projects so there won't be problems.

I have two tables that are related in many ways, they are playlists and genres , in the production database they generate playlists_genres > with the following Table of fields:

Note that these fields are similar to playlistsid and genresid

In the golang project, the m2m results of the table are as follows:

Please note that the generated fields are different from what I need, I need to generate identifiers for the database in production, which 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 checked a lot of information, and I haven't found a way to change the names of these columns other than manually creating an intermediate table with the columns with the names I need and correlating them from one to many.

Will you help me with this problem? Is it possible to do this without manually creating the table? Thanks!

Workaround

Unfortunately, I think the only way to achieve your goal is to create a structure called PlaylistGenre and override the column names here.
Using the Gorm convention, you can only operate on foreign key constraints created in the database. There is no chance to overwrite the actual column names of the automatically created join table.
In the gorm annotation, you can use these properties to handle foreign keys: foreignKey, references, joinForeignKey, and joinReferences.

However, you need to use column, which is not possible here.

If you need an example of this approach, please let me know!

The above is the detailed content of How to change result table column name from "many" to "many" in gorm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete