fix: 复合主键表 shop_players 的 update 改用原生 SQL
ent 的 Update().Where() 对复合主键表会报 "sql/sqlgraph: invalid composite id for update table",这是 ent 框架已知的限制(field.ID 复合主键仅对 edge schema 完整支持)。同表的 Create/Delete/Query 操作不受影响,仅 Update 需要绕过。在 ServiceContext 中暴露底层 *sql.DB 以供 ExecContext 执行原生 SQL。
This commit is contained in:
@@ -3,8 +3,6 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/shop/rpc/internal/models/predicate"
|
||||
"juwan-backend/app/shop/rpc/internal/models/shopplayers"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/shop/rpc/internal/svc"
|
||||
@@ -32,25 +30,28 @@ func (l *UpdateShopPlayersLogic) UpdateShopPlayers(in *pb.UpdateShopPlayersReq)
|
||||
return nil, errors.New("invalid shop_id or player_id")
|
||||
}
|
||||
|
||||
preds := []predicate.ShopPlayers{
|
||||
shopplayers.ShopIDEQ(in.ShopId),
|
||||
shopplayers.PlayerIDEQ(in.PlayerId),
|
||||
}
|
||||
|
||||
updater := l.svcCtx.ShopModelRW.ShopPlayers.Update().
|
||||
Where(shopplayers.And(preds...)).
|
||||
SetIsPrimary(in.IsPrimary)
|
||||
// ent 的 Update().Where() 不支持复合主键表(shop_players 的主键是 (shop_id, player_id)),
|
||||
// 会报 "sql/sqlgraph: invalid composite id for update table"。
|
||||
// Create/Delete/Query 不受影响,仅 Update 需要走原生 SQL。
|
||||
query := `UPDATE shop_players SET is_primary = $1, left_at = $2 WHERE shop_id = $3 AND player_id = $4`
|
||||
|
||||
var leftAt *time.Time
|
||||
if in.LeftAt > 0 {
|
||||
t := time.Unix(in.LeftAt, 0)
|
||||
updater = updater.SetLeftAt(t)
|
||||
leftAt = &t
|
||||
}
|
||||
|
||||
affected, err := updater.Save(l.ctx)
|
||||
result, err := l.svcCtx.DBRW.ExecContext(l.ctx, query, in.IsPrimary, leftAt, in.ShopId, in.PlayerId)
|
||||
if err != nil {
|
||||
logx.Errorf("update shop players failed, %s", err.Error())
|
||||
return nil, errors.New("update shop players failed")
|
||||
}
|
||||
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
logx.Errorf("get rows affected failed, %s", err.Error())
|
||||
return nil, errors.New("update shop players failed")
|
||||
}
|
||||
if affected == 0 {
|
||||
return nil, errors.New("shop player not found")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user