Server Side Swift with Vapor - Part 23: Creating | Ray Wenderlich

Learn how to send data with web forms and accept the data back in our Vapor application to create models.


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

A few notes about this video that might help clear up confusion:

  1. You can also re-use the existing AllUsersContext (or whatever you called it in your code) that was previously created to display the list of users instead of creating a new CreateAcronymContext for creating a user. Correct me if I’m wrong, but I believe the only reason to create a new context is to keep the contexts separate in case you need to make changes to one screen or the other in the future …

  2. The instructions to add a new init method to the Acronym class might be confusing to those who followed along since I believe this was already done previously for another video. (I would have to look it up to see which one exactly. Might have been a challenge and that is why it was re-done here but for those who did the challenges, this might be very confusing …)

  3. Also at about 6:30 there is a slight duplication in audio where it says “to perform a redirect …” twice :slight_smile:

1 - Yep you can. It does get reused for the Edit page, which becomes even more helpful if on errors you pass the acronym back to the page to pre-populate the fields etc. But I didn’t want to confuse people too much!

2 - Ah yeah, that’s my bad. In the original betas (and Swift 4.0) when the videos were originally recorded, if you declared a class Codable you didn’t need to provide an initialiser. That isn’t the case any more in 4.1 :disappointed:

3 - Thanks for letting me know!

Thanks for the feedback! :smiley:

Happy to help :slight_smile: As I mentioned before, I’m really enjoying the video series and am looking forward to running a few apps on my server.

Of course, before that, I need to compile Swift for CentOS since my server runs CentOS. Been compiling for the last few hours - not sure if it will work or not since others have reported mixed success in compiling Swift for different Linux platforms :smiley:

@fahim yeah unfortunately we’re kinda at the mercy of Apple there for different Linux support. Theoretically it should all work, but people have had issues with edge cases with glibc versions. Honestly I would just run it in a Docker container and then you can run it on whatever OS you want!

That’s my plan B if I fail to get things to compile correctly. It just finished the initial build file creation successfully. So I’m still being hopeful, but you know how it is … Guess I’ll find out soon enough :stuck_out_tongue:

when I Submit on the Create An Acronym page, the page loads to “http://localhost:8080/acronyms/11” and displays “Oops: This parent relationship could not be resolved”.

The home page still displays all the acronyms I add. I did “vapor xcode -y” on the Download Materials example project and ran it but it does the same thing.

Xcode says:
Migrating mysql DB
Migrations complete
Server starting on http://localhost:8080
[ ERROR ] FluentError.parentRequired: This parent relationship could not be resolved (Parent.swift:38)

@doyle hmm sounds like your database has been mismatched. Have you been been moving back and forth between projects. Basically it sounds like the userID column in missing. What you can try is kill the database and starting it up again:

docker stop mysql
docker rm mysql
docker run --name mysql -e MYSQL_USER=til -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor -p 3306:3306 -d mysql/mysql-server

Rerun your app and it should work.

Thanks, tried that but still gives the same error.

I can move around the site to the various pages but when I click submit and get the error, I can no longer load any of the pages and have run Xcode again. Once reloaded the previous acronym shows up.

I cleaned the project and deleted derived data. Restarted the mac, dowloaded a fresh copy of the download material, redid the docker stop and rm steps. Getting the same error.

@doyle that is weird. If you use the API does it work? And if you connect to the MySQL DB via the command line can you inspect the table structure - does it have the userID column in it?

After you downloaded the materials did you run vapor update to get the latest RC? I don’t think the download materials have been updated as they are waiting for the new videos to go up.

Got it working using your instructions in the part 22 forum:

1 Like