Server Side Swift with Vapor - Part 8: Models And | Ray Wenderlich

In this video you will learn how to create models using Fluent, and how to configure your databases.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/8

Why aren’t you using the --template=api option?

I created my project just like you did, skipped touching the acronym file, and then generated the Xcode project. I changed the target/build to Run and My Mac and hit build. I get 98 errors.

I’m using vapor toolbox 3.1.4 and vapor framework 3.0.0-rc.1

@gargoyle vapor new automatically defaults to the API template so there’s no need to add it.

What are the errors you’re getting? You need to be on Swift 4.1

Yup, I forgot to set the toolchain to 4.1!

1 Like

@0xtim I created the Acronym.swift file via Xcode and went through the steps.

I’m receiving a Use of unresolved Identifier 'Acronym' error even though the file is there? I’m not sure what is wrong. Using the toolchain 4.1 and vapor-3.0.0-rc-1

Edit: Figured it out, when creating the file from Xcode it sets the Target Membership to your project name.
You need to uncheck that and set the target to be App. Then it should work

You change this in the File Inspector in the Utilities bar (right side bar).

@simonqq yeah SPM and Xcode don’t have the nicest integration yet. That’s generally why it’s a good idea to create the files via the command line. Be careful where you put the file because if it’s not in the right directory then when you next regenerate your project it may break again!

Hi. Why there is no caption (CC) in all Videos ? It helps a lot specially when you want to keep the sound volume low… :frowning:

@ffabri good question - I’ll ask

@ffabri they’re definitely coming and will be up in the next few weeks. We’re just waiting for Vapor 3 to come out of beta.

Alright then. Thank you !

@0xtim
please add subtitle for this video

@lashkari subtitles are being done now - there was a delay whilst we waited for Vapor to settle down, should be up in the next week or so I believe

Hi, I’m getting 21 errors with Xcode Version 9.2 (9C40b), Toolchain: Swift 4.1 Snapshot 2018-03-11 (a)
Acronym.swift has App checked as the target


@subhransu haven’t seen that error before! In Terminal in the project directory (close Xcode) can you run rm -rf .build/ *.xcodeproj/ Package.resolved && vapor xcode -y and try again?

Hey Tim!

I did everything you did, and as soon as I run like you did at the end of the second video for creating an API section, I get the following error: Contextual type ‘StaticString’ cannot be used with array literal. I’m not sure how to fix this and the answers I’m finding when I google don’t really make sense.

Thanks so much!

@opusmagnum77 can you paste the code that is throwing that error?

@0xtim Tim, I think I missed something in one of these videos. Let’s say I have models of Client and Usage. In my Client class I have a method which, to complete, needs to query the Usage table. I don’t have a Request object at this point though…how do I query the other table?

My Client has this:

var usage: Children<Client, Usage> {
    return children(\.clientID)
}

and my Usage class has a ‘month’ property, so I want to be able to do something like this:

func someMethod() {
    let calendar = Calendar(identifier: .gregorian)
    let lastMonth = calendar.date(byAdding: .month, value: -1, to: month)!
    let lastMonthUsage = children.filter { $0.month == lastMonth }.first

@gargoyle basically you need to pass in a Request or DatabaseConnectable to someMethod. This might be something you want to do in the controller instead