Vapor - 更新用户 属性 returns "Precondition failed - id.exists"
Vapor - updating user property returns "Precondition failed - id.exists"
我已尝试将 UUID 添加到用户模型上的 UUID 数组 属性,但它 returns“前提条件失败 - 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()
}
}
我发现了为什么会出现这个问题。我像那样从 jwt 中获取用户 - let user = try req.auth.require(AppUser.self)
然后我修改它并将其保存到数据库中。我所做的是从数据库中获取用户,对其进行修改,然后将其保存到数据库中。它说当我使用第一个选项时我无法更新或保存用户,因为来自 jwt 的用户就像全新的,而 postgres 表现得很疯狂,因为已经有一个用户使用这个 ID。
我已尝试将 UUID 添加到用户模型上的 UUID 数组 属性,但它 returns“前提条件失败 - 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()
}
}
我发现了为什么会出现这个问题。我像那样从 jwt 中获取用户 - let user = try req.auth.require(AppUser.self)
然后我修改它并将其保存到数据库中。我所做的是从数据库中获取用户,对其进行修改,然后将其保存到数据库中。它说当我使用第一个选项时我无法更新或保存用户,因为来自 jwt 的用户就像全新的,而 postgres 表现得很疯狂,因为已经有一个用户使用这个 ID。