3
votes

In my old Vapor 3 code I'm creating models like this:

address.create(on: conn).flatMap { savedAddress in
    // do something with saved address
}

With the help of the Vapor 4 documentation I'm now passing db to the create function instead of a connection. The issue is, that it seems that flatMap returns Void as I'm getting the following error when I try to access savedAddress: "Value of tuple type 'Void' has no member ...".

How should I rewrite this?

1
Hey you could find me in Discord as iMike#3049 and I'll help you with migration to Vapor 4imike
Since Fluent4 returns Void on new record creation the workaround is to set id to new record in advance and then retrieve that record from the database by that id.imike
This sounds like a step backward, that was much easier in v3. Is there really no better solution?G. Marc
If you prefer pure SQL then I could suggest Bridges github.com/SwifQL/Bridgesimike
I think I'll wait a couple of days with upgrading the server till there is an official upgrade guide for fluent. If I still have issues, I will take your offer to help me. Thank you so much for this.G. Marc

1 Answers

4
votes

Because all models in Fluent 4 must be classes, you can rely on reference semantics to do what you want (which is why create(on:) returns EventLoopFuture<Void>. So in your example, instead of savedAddress you can just use address as the saved model. When it has been saved, Fluent will set the ID property of your model on the address object, which you can then retrieve in your flatMap closure with address.requireID()