I've tried to add UUID to UUID array property on users model but it returns "Precondition failed - id.exists". I'm using update on database not create or save. Using PostgresSQL for database. The error is on FluentKit -> Model -> Model+CRUD.swift -> _ update(on: ) line 43. Attaching the code below.
User model:
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
}
}
Request:
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)
}
}
}
Migration:
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()
}
}
I found out why that issue is coming up. I was taking the user from the jwt like that - let user = try req.auth.require(AppUser.self)
and after that I was modifying it and saving it to the database. What i did was to fetch the user from the database, modify it and after that save it to the database. It was saying that i can't update or save the user when I used the first option because the user from the jwt was like brand new and postgres was acting crazy because there was already a user with this id.