Server Side Swift with Vapor - Part 13: Sibling | Ray Wenderlich

I’m following along with the book and the updated beta using PostgreSQL. Here is my code:

    func save(_ request : Request) throws -> Future<HTTPStatus> {
    return try flatMap(to: HTTPStatus.self, request.parameters.next(Marker.self), request.parameters.next(Tag.self)) { marker, tag in
            let marker = marker.save(on: request)
            let pivot = try MarkerTag(marker.requireID(), tag.requireID())

            pivot.save(on: request)

            return marker.transform(to: .created)
        }
}

I’m getting the following error on the line with “let pivot 
”

Value of type ‘EventLoopFuture’ has no member ‘requireID’

Any thoughts what might be causing this? (I’m intentionally updating the pivot and the model in the same request.)

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@mikebronner save(on:) returns a future (since it needs to wait for the save to return from the DB) so you need to unwrap wrap. Is there any particular reason why you’re saving the marker again? You’re effectively getting the marker from the database and then saving it again with no changes. You’re also throwing away the result of the save on the pivot - if that fails you wouldn’t know about it. I would write it with:

func save(_ request : Request) throws -> Future<HTTPStatus> {
  return try flatMap(to: HTTPStatus.self, request.parameters.next(Marker.self), request.parameters.next(Tag.self)) { marker, tag in
    let pivot = try MarkerTag(marker.requireID(), tag.requireID())

    return pivot.save(on: request).transform(to: .created)
  }
}

Hello. When creating the first Pivot of the course you will be asked to create a protocol, but I am in trouble understanding how to fix it. In this case, what will be added and the error will be resolved?

import Foundation
import Vapor
import FluentSQLite


final class AcronymCategoryPivot:SQLiteUUIDPivot {

    var id:UUID?
    var acronymID:Acronym.ID
    var categoryID :Category.ID
    
    typealias Left = Acronym
    typealias Right = Category
    
    static let leftIDKey: LeftIDKey = \AcronymCategoryPivot.acronymID
    static let rightIDkey:RightIDKey = \AcronymCategoryPivot.categoryID
    
    init(_ acronymID:Acronym.ID, _ categoryID:Category.ID) {
        self.acronymID = acronymID
        self.categoryID = categoryID
    }
}

extension AcronymCategoryPivot:Migration {}

Hmm that looks good to me. If you click on the dot in Xcode and click add stubs, what does it think it’s missing?

It seems necessary to add this code and edit some code.

Ah I’ve spotted the issue! In your original declaration you have a rightIDkey variable - note the lower case k in key - it should be an upper case K!

Thank you. It began to move.

Can someone elaborate a little on “switch to using MySQL” for the people who’ve never done web development before? Isn’t as straight forward as swapping import statements


For the basic stuff you change your dependencies to bring in MySQL, change the import statements and then change your SQLiteModels to MySQLModels. If you’re doing anything custom, you’ll need to migrate that as well

I was getting the following response:

{
“error”: true,
“reason”: “Could not convert to Int: 
”
}

But after I changed SQLiteUUIDPivot to SQLiteModel, Pivot and the ID Type from UUID to Int, it’s working.

I think there is only an issue the SQLiteUUIDPivot class because the SQLiteUUIDModel class is working properly.

@jmarkstar Thank you for sharing your solution - much appreciated! :]