Server Side Swift with Vapor - Part 14: Fluent | Ray Wenderlich

It’s not a sibling (I don’t think) because one Point has many OrderPoints, but an OrderPoint only has one Point. So it’s a one to many.

In that case is there any reason why you haven’t modelled it slightly differently and added the point_id as a column to the OrderPoint table?

points_id is a column on the OrderPoint table.

UhAuM

What’s the relationship between between points and orders?

Think of ‘Points’ table as a list of things you could order. I’m using the names the client wanted so don’t blame me :] So the Points would have a ‘sweeper’ you could order at a cost of 2, or a ‘promo’ at a cost of 15. In my ‘Order’ I might want 3 sweepers and 1 promo. So my total points would end up at (3 * 2) + (1 * 15)

So there’d be 1 Order item, 2 OrderPoints linked to the order, and then each OrderPoints would each have a single Points linked to it.

Sounds like you need to organise the relationships yourself! Take a look at how joins are done in the sibling code to give you some pointers!

Hmm - I appreciate the updates to searchHandler however the compiler is unable to resolve \.short and \.long. I get, “Type of expression is ambiguous without more context”.

func searchHandler(_ req: Request) throws -> Future<[Acronym]> {
    guard let searchTerm = req.query[String.self, at: "term"] else {
        throw Abort(.badRequest)
    }
    return Acronym.query(on: req).group(.or) { or in
        or.filter(\.short == searchTerm)
        or.filter(\.long == searchTerm)
        }.all()
}
1 Like

@gonzalo have you got import Fluent in the file?

1 Like

Good grief. That was it. Thank you!

1 Like

Interesting enough, I didn’t need to import Fluent to use sort but I one needs to in order to use filter :thinking:

It’s the filter operators that aren’t automatically imported (like == to compare a column and value)

1 Like

Thank you for the reply