Gorm创建并返回多对多关系的值

huangapple go评论85阅读模式
英文:

Gorm create and return value many2many

问题

我想创建一个数据并返回该值,但该值与另一个表相关。

User response struct

type UserRespone struct {
	ID        int           `json:"id,omitempty"`
	Email     string        `json:"email"`
	FirstName string        `json:"first_name"`
	LastName  string        `json:"last_name"`
	Address   string        `json:"address"`
	Roles     []roles.Roles `json:"roles" gorm:"many2many:users_roles;foreignKey:ID;joinForeignKey:UserID;references:ID;joinReferences:RolesID"`
}

Roles struct

type Roles struct {
	ID   int `json:"id" gorm:"<-:false;primaryKey"`
	Name string
}

这是一个创建数据的函数,我想返回值[]roles.Roles

func (r *UserRepositoryImpl) AuthSignUp(ctx context.Context, req user.AuthSignUp) (user.UserRespone, error) {
	createdUser := req.ToUser()
	
	hashPassword, _ := bcrypt.GenerateFromPassword([]byte(createdUser.Password), bcrypt.DefaultCost)

	createdUser.Password = string(hashPassword[:])

	result := r.Db.
		WithContext(ctx).
		Create(&createdUser)

	for _, rolesID := range req.RolesID {
		userRole := new(user.UsersRoles)

		userRole.UserID = createdUser.ID
		userRole.RolesID = rolesID
		result = r.Db.WithContext(ctx).Create(&userRole)
	}

	return createdUser.ToResponse(), result.Error
}

我想返回以下值:

user.UserResponse{
	ID:        4,
	Email:     "putri4@gmail.com",
	FirstName: "putri",
	LastName:  "cantik",
	Address:   "bonang",
	Roles: []roles.Roles{
		{
			ID:   1,
			Name: "Admin",
		},
		{
			ID:   2,
			Name: "Consumer",
		},
	},
}

但是当我创建数据集时,我只得到以下值:

user.UserRespones{
	ID:        4,
	Email:     "putri4@gmail.com",
	FirstName: "putri",
	LastName:  "cantik",
	Address:   "bonang",
	Roles:     []roles.Roles(nil),
}
英文:

I want to create a data and then return the value, but the value is related to another table.

User response struct

type UserRespone struct {
	ID        int           `json:&quot;id,omitempty&quot;`
	Email     string        `json:&quot;email&quot;`
	FirstName string        `json:&quot;first_name&quot;`
	LastName  string        `json:&quot;last_name&quot;`
	Address   string        `json:&quot;address&quot;`
	Roles     []roles.Roles `json:&quot;roles&quot; gorm:&quot;many2many:users_roles;foreignKey:ID;joinForeignKey:UserID;references:ID;joinReferences:RolesID&quot;`
}

Roles struct

type Roles struct {
	ID   int `json:&quot;id&quot; gorm:&quot;&lt;-:false;primaryKey&quot;`
	Name string
}

And this is a function to create a data, and I want to return the value [ ]roles.Roles

func (r *UserRepositoryImpl) AuthSignUp(ctx context.Context, req user.AuthSignUp) (user.UserRespone, error) {
	createdUser := req.ToUser()
	
	hashPassword, _ := bcrypt.GenerateFromPassword([]byte(createdUser.Password), bcrypt.DefaultCost)

	createdUser.Password = string(hashPassword[:])

	result := r.Db.
		WithContext(ctx).
		Create(&amp;createdUser)

	for _, rolesID := range req.RolesID {
		userRole := new(user.UsersRoles)

		userRole.UserID = createdUser.ID
		userRole.RolesID = rolesID
		result = r.Db.WithContext(ctx).Create(&amp;userRole)
	}

	return createdUser.ToResponse(), result.Error
}

I want to return the value like this:

user.UserResponse{
		ID:        4,
		Email:     &quot;putri4@gmail.com&quot;,
		FirstName: &quot;putri&quot;,
		LastName:  &quot;cantik&quot;,
		Address:   &quot;bonang&quot;,
		Roles: []roles.Roles{
			{
				ID: 1,
				Name: &quot;Admin&quot;,
			},
			{
				ID: 2,
				Name: &quot;Consumer&quot;,
			},
		},
	}

But when I create a data set, I only get values like :

user.UserRespones{
		ID:        4,
		Email:     &quot;putri4@gmail.com&quot;,
		FirstName: &quot;putri&quot;,
		LastName:  &quot;cantik&quot;,
		Address:   &quot;bonang&quot;,
		Roles:     []roles.Roles(nil),
	}

答案1

得分: 2

据我所知,您已经拥有用户数据和角色ID。我猜您只是想获取角色名称:

err := r.db.Find(&amp;createdUser.Roles, req.RolesID)
// 错误处理

话虽如此,您的类型和名称有点不清楚。UserRespone(注意拼写错误)应该像DBUser这样命名-无论它是用作响应还是其他用途,它都代表了用户的数据库条目,对吗?

此外,我只能对createdUserUsersRoles的类型和字段进行假设,所以可能会漏掉一些东西。

我猜createdUser的类型是UserRespone-但是我希望Roles已经完整(您根本不需要查询任何内容)。如果不是这样,那么您应该引入一个新类型,例如RequestUser(与DBUser相对),它只包含Role的ID而不包含Role的名称。将请求数据、数据库条目和响应数据挤入同一类型中会令人困惑(对我来说有点过于紧密耦合)。

英文:

As far as I can tell, you already have the user data and the role ids. I figure you just want to get the role names as well:

err := r.db.Find(&amp;createdUser.Roles, req.RolesID)
// err handling

That said, your types and names are a bit unclear. UserRespone (note the typo) should probably named like DBUser - it isn't important whether it is used as response or something else, it represents the database entry for an user, right?

Additionally, I can only make assumptions about the type and fields of createdUser and UsersRoles, so I might have missed something.

I figure createdUser is of type UserRespone - but then I would expect that Roles is already complete (and you don't have to query anything at all). If not, then you should introduce a new type for that, e.g. RequestUser (as opposed to DBUser), which only contains Role ids but not Role names. Squeezing the data from the request, the db entry and the response data into the same type is confusing (and a tad too tightly coupled for my taste).

huangapple
  • 本文由 发表于 2022年11月12日 16:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74411293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定