GORM Association Append 在 MySQL 上工作,但在 SQLite 上出现错误

问题描述 投票:0回答:1

由于各种原因,我在创建服务器后必须将数据库更改为SQLite。当我在切换到 SQLite 后测试我的服务器时,

Association().Append()
现在抛出了一个

不明确的列名:id

错误。这是我的代码的样子:

func (*AccountServer) UpdateAccountOwnedEdgeDevice(ctx context.Context, req *pb.UpdateAccountOwnedEdgeDeviceRequest) (*pb.UpdateAccountOwnedEdgeDeviceResponse, error) {
    var account models.Account

    toInsertEdgeDevice := &models.EdgeDeviceInfo{
        ID:          req.EdgeDevice.Id,
        Name:        req.EdgeDevice.Name,
        Description: req.EdgeDevice.Description,
    }

    res := cmmHelpers.GetDB().First(&account, "id = ?", req.Id)

    if res.RowsAffected == 0 {
        return nil, errors.New("accounts not found")
    }

    err := res.Association("OwnedEdgeDevices").Append(
        toInsertEdgeDevice,
    )

    if err != nil {
        return nil, err
    }

    return &pb.UpdateAccountOwnedEdgeDeviceResponse{
        Success: true,
    }, nil
}

GORM 也显示此查询:

更新

accounts
设置
updated_at
="2023-11-29 14:30:20.428" 来自
accounts
其中 id =“7e8c7eba-a676-4743-8e28-75d02f105555”并且
accounts
.
deleted_at
为 NULL 并且
id
= “7e8c7eba-a676-4743-8e28-75d02f105555”

我刚刚意识到与

account
相关的另一个 API 端点也抛出了错误。
account
的模型如下所示:

type Account struct {
    gorm.Model
    ID                 string           `gorm:"primarykey; size:40;"`
    FirstName          string           `json:"first_name"`
    LastName           string           `json:"last_name"`
    UserName           string           `gorm:"unique; not null" json:"user_name"`
    Password           string           `gorm:"column:password"`
    Email              *string          `binding:"required" json:"email"`
    OwnedEdgeDevices   []EdgeDeviceInfo `gorm:"many2many:user_owned_edge_devices;" json:"owned_edge_devices"`
    NeedPasswordChange bool             `gorm:"default:false;" json:"need_password_change"`
}

这是我想要与之关联的模型:

type EdgeDeviceInfo struct {
    gorm.Model
    ID          string  `gorm:"primarykey; size:40;"`
    Name        string `gorm:"not null" json:"name"`
    Description string  `json:"description"`
    Status      bool    `gorm:"default:false; column:statusz"`
    APIKey      string  `json:"api_key"`
    Passcode    string  `gorm:"default:0000"`
}
sqlite go-gorm
1个回答
0
投票
UPDATE accounts SET updated_at="2023-11-29 14:30:20.428" FROM accounts WHERE id = "7e8c7eba-a676-4743-8e28-75d02f105555" AND accounts.deleted_at IS NULL AND id = "7e8c7eba-a676-4743-8e28-75d02f105555"

这是代码的修改版本:

func (*AccountServer) UpdateAccountOwnedEdgeDevice(ctx context.Context, req *pb.UpdateAccountOwnedEdgeDeviceRequest) (*pb.UpdateAccountOwnedEdgeDeviceResponse, error) {
    var account models.Account

    toInsertEdgeDevice := &models.EdgeDeviceInfo{
        ID:          req.EdgeDevice.Id,
        Name:        req.EdgeDevice.Name,
        Description: req.EdgeDevice.Description,
    }

    res := cmmHelpers.GetDB().First(&account, "id = ?", req.Id)

    if res.RowsAffected == 0 {
        return nil, errors.New("account not found")
    }

    // Append the edge device to the association
    err := cmmHelpers.GetDB().Model(&account).Association("OwnedEdgeDevices").Append(toInsertEdgeDevice)

    if err != nil {
        return nil, err
    }

    return &pb.UpdateAccountOwnedEdgeDeviceResponse{
        Success: true,
    }, nil
}

这段代码中删除了Account模型的更新,只是改变了关联。 GORM 不会以这种方式为 Account 模型生成不需要的

UPDATE
语句。

© www.soinside.com 2019 - 2024. All rights reserved.