2

我尝试将 UUID 添加到用户模型上的 UUID 数组属性,但它返回“前提条件失败 - id.exists”。我在数据库上使用更新而不是创建或保存。使用 PostgresSQL 作为数据库。错误出现在 FluentKit -> Model -> Model+CRUD.swift -> _ update(on: ) 第 43 行。附上下面的代码。

用户型号:

final class AppUser: Model, Content {
    static let schema: String = "users"
    
    @ID(key: .id)
    var id: UUID?

    @Field(key: "email")
    var email: String
    
    @Field(key: "passwordHash")
    var passwordHash: String

    @Field(key: "plantIds")
    var plantIds: [UUID]

    @Field(key: "sharedPlantIds")
    var sharedPlantIds: [UUID]
    
    init() {}
    
    init(id: UUID? = nil, email: String, passwordHash: String, plantIds: [UUID] = [UUID](), sharedPlantIds: [UUID] = [UUID]()){
        self.id = id
        self.email = email
        self.passwordHash = passwordHash
        self.plantIds = plantIds
        self.sharedPlantIds = sharedPlantIds
    }
}

要求:

func addOwnPlant(req: Request) throws -> EventLoopFuture<Response> {
    let user = try req.auth.require(AppUser.self)
    let reqPlant = try req.content.decode(AppUserPlantCreateRequest.self)
    let uuid = UUID()
    print(uuid)
    let newPlant = AppUserPlant(id: uuid, parentId: reqPlant.parentId, notes: reqPlant.notes, timesPlantIsWatered: reqPlant.timesPlantIsWatered, name: reqPlant.name, lastTimeWatered: reqPlant.lastTimeWatered)
       
    return newPlant.save(on: req.db).flatMap { _ in
        guard let id = newPlant.id else {
            return DataWrapper.encodeResponse(data: Fail.init(message: "no id"), for: req)
        }
        user.plantIds.append(id)
        return user.update(on: req.db).flatMap {
            return DataWrapper.encodeResponse(data: newPlant.newPlantResposne, for: req)
        }
    }
}

移民:

struct AppUserMigration: Migration {
    var name: String {"Users migration"}
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("users")
            .field("id", .uuid)
            .field("email", .string, .required)
            .field("passwordHash",.string,.required)
            .field("plantIds", .array(of: .uuid))
            .field("sharedPlantIds", .array(of: .uuid))
            .unique(on: "email")
            .create()
    }
    func revert(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("users").delete()
    }
}
4

1 回答 1

2

我发现了为什么会出现这个问题。我像这样从 jwt 中获取用户 -let user = try req.auth.require(AppUser.self)之后我正在修改它并将其保存到数据库中。我所做的是从数据库中获取用户,对其进行修改,然后将其保存到数据库中。有人说当我使用第一个选项时我无法更新或保存用户,因为来自 jwt 的用户就像全新的,而 postgres 表现得很疯狂,因为已经有一个具有此 ID 的用户。

于 2021-08-14T22:24:53.977 回答