fix: 复合主键表改为单字段 id 以支持 ent 完整生成

This commit is contained in:
zetaloop
2026-04-24 07:32:52 +08:00
parent c62d743320
commit 5ad579f03c
19 changed files with 324 additions and 82 deletions
+61 -15
View File
@@ -764,6 +764,7 @@ type ShopPlayersMutation struct {
config
op Op
typ string
id *int64
shop_id *int64
addshop_id *int64
player_id *int64
@@ -796,16 +797,35 @@ func newShopPlayersMutation(c config, op Op, opts ...shopplayersOption) *ShopPla
return m
}
// withShopPlayersID sets the ID field of the mutation.
func withShopPlayersID(id int64) shopplayersOption {
return func(m *ShopPlayersMutation) {
var (
err error
once sync.Once
value *ShopPlayers
)
m.oldValue = func(ctx context.Context) (*ShopPlayers, error) {
once.Do(func() {
if m.done {
err = errors.New("querying old values post mutation is not allowed")
} else {
value, err = m.Client().ShopPlayers.Get(ctx, id)
}
})
return value, err
}
m.id = &id
}
}
// withShopPlayers sets the old ShopPlayers of the mutation.
func withShopPlayers(node *ShopPlayers) shopplayersOption {
return func(m *ShopPlayersMutation) {
m.oldValue = func(context.Context) (*ShopPlayers, error) {
return node, nil
}
m.predicates = append(m.predicates,
shopplayers.ShopIDEQ(node.ShopID),
shopplayers.PlayerIDEQ(node.PlayerID),
)
m.id = &node.ID
}
}
@@ -828,12 +848,38 @@ func (m ShopPlayersMutation) Tx() (*Tx, error) {
return tx, nil
}
// SetID sets the value of the id field. Note that this
// operation is only accepted on creation of ShopPlayers entities.
func (m *ShopPlayersMutation) SetID(id int64) {
m.id = &id
}
// ID returns the ID value in the mutation. Note that the ID is only available
// if it was provided to the builder or after it was returned from the database.
func (m *ShopPlayersMutation) ID() (id int64, exists bool) {
if m.id == nil {
return
}
return *m.id, true
}
// IDs queries the database and returns the entity ids that match the mutation's predicate.
// That means, if the mutation is applied within a transaction with an isolation level such
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
// or updated by the mutation.
func (m *ShopPlayersMutation) IDs(ctx context.Context) ([]int64, error) {
return nil, fmt.Errorf("IDs is not supported on %s operations for ShopPlayers", m.op)
switch {
case m.op.Is(OpUpdateOne | OpDeleteOne):
id, exists := m.ID()
if exists {
return []int64{id}, nil
}
fallthrough
case m.op.Is(OpUpdate | OpDelete):
return m.Client().ShopPlayers.Query().Where(m.predicates...).IDs(ctx)
default:
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op)
}
}
// SetShopID sets the "shop_id" field.
@@ -858,8 +904,8 @@ func (m *ShopPlayersMutation) OldShopID(ctx context.Context) (v int64, err error
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldShopID is only allowed on UpdateOne operations")
}
if m.oldValue == nil {
return v, errors.New("OldShopID requires the old ShopPlayers value in the mutation")
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldShopID requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -914,8 +960,8 @@ func (m *ShopPlayersMutation) OldPlayerID(ctx context.Context) (v int64, err err
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldPlayerID is only allowed on UpdateOne operations")
}
if m.oldValue == nil {
return v, errors.New("OldPlayerID requires the old ShopPlayers value in the mutation")
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldPlayerID requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -969,8 +1015,8 @@ func (m *ShopPlayersMutation) OldIsPrimary(ctx context.Context) (v bool, err err
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldIsPrimary is only allowed on UpdateOne operations")
}
if m.oldValue == nil {
return v, errors.New("OldIsPrimary requires the old ShopPlayers value in the mutation")
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldIsPrimary requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -1018,8 +1064,8 @@ func (m *ShopPlayersMutation) OldJoinedAt(ctx context.Context) (v time.Time, err
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldJoinedAt is only allowed on UpdateOne operations")
}
if m.oldValue == nil {
return v, errors.New("OldJoinedAt requires the old ShopPlayers value in the mutation")
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldJoinedAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
@@ -1054,8 +1100,8 @@ func (m *ShopPlayersMutation) OldLeftAt(ctx context.Context) (v *time.Time, err
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldLeftAt is only allowed on UpdateOne operations")
}
if m.oldValue == nil {
return v, errors.New("OldLeftAt requires the old ShopPlayers value in the mutation")
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldLeftAt requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {