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

PHPz
Release: 2024-02-10 13:45:08
forward
999 people have browsed it

如何在 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 areplaylistsandgenres, in the production database they generateplaylists_genres> with the following Table of fields:

Note that these fields are similar toplaylistsidandgenresid

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 isplaylistsidandgenresid.

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"` }
Copy after login

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"` }
Copy after login

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 calledPlaylistGenreand 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 thegormannotation, you can use these properties to handle foreign keys:foreignKey,references,joinForeignKey, andjoinReferences.

However, you need to usecolumn, 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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!