Home > front end >  Create 1-to-1 relationship in Vapor Fluent asynchronously
Create 1-to-1 relationship in Vapor Fluent asynchronously

Time:08-29

Vapor Fluent question. I have the following code to save a User to a Postgres.

User
@Parent(key: FieldKeys.profile) var profile: Profile

...

let profile = User.Profile()
try await profile.save(on: request.db)
                            
if let profileID = try await User.Profile.query(on: request.db).all().last?.id {
    user.$profile.id = profileID; print("Profile created with ID=\(profileID)")
}
try await user.save(on: request.db)

My concern here is that another request could come in at the same time and create an entry in the Profile table and its id will have been returned as the last instead of the previous entry's id.

Is there a guaranteed way to create a user-profile relationship?

CodePudding user response:

Vapor has the ability to create transactions so you can be certain nothing will intervene. Try something like:

try await request.db.transaction { database in
    let user = try await User.query...
    let profile = Profile()
    try await profile.save(on: database)
    user.$profile.id = profile.id
    try await user.save(on: database)
}

CodePudding user response:

When you save a model in Fluent the ID is populated for you from the returned data in the query. So you can write

let profile = User.Profile()
try await profile.save(on: request.db)

let profileID = try profile.requireID()         
user.$profile.id = profileID
try await user.save(on: request.db)

You could also use one of the attach helpers from Fluent

  • Related