Server Side Swift with Vapor - Part 22: Challenge: | Ray Wenderlich

Ahh ok that sounds like a MySQL pipelining issue, or similar to the double flatMap issue - Iā€™ve got an issue open on GitHub and it should be fixed when the NIO stuff gets merged in

Hi @0xtim,

Thanks very much for another great tutorial!

I completed the challenge without issues, but when I went to create acronym/category relationship via:
POST /api/acronyms/1/categories/1 - I encountered an error - ā€œFluentError.modelNotFound: No model with ID 1 was found (Model.swift:267)ā€

Both of these endpoints return results without a problem though:
GET /api/acronyms/1
GET /api/categories/1

I suspect this error came about after changing to persisted storage, as it was working before.

I thought perhaps some migration didnā€™t take place, but I can see all tables in mySql:

+------------------+
| Tables_in_vapor  |
+------------------+
| acronym+category |
| acronyms         |
| categories       |
| fluent           |
| users            |
+------------------+

Would appreciate any help :).

Thanks!

@brokenzz you havenā€™t done anything wrong! Thereā€™s a bug in the current MySQL beta to do with pipelining queries, basically the double flatMap() is causing issues. Until this is resolved you can instead split out the parameter extractions:

func addCategoriesHandler(_ req: Request) throws -> Future<HTTPStatus> {
  return try req.parameter(Acronym.self).flatMap(to: HTTPStatus.self) { acronym in
    return try req.parameter(Category.self).flatMap(to: HTTPStatus.self) { category in
      let pivot = try AcronymCategoryPivot(acronym.requireID(), category.requireID())
      return pivot.save(on: req).transform(to: .ok)
    }
  }
}

That should work

@0xtim :ok_hand: Worked like a charm! Thank you! :pray:

1 Like

Hi, Iā€™m doing the challenge and getting MySQL error however before implementing the fix for addCategoriesHandler, I did vapor update and suddenly Iā€™m getting build error.

I tried rm -rf .build/ *.xcodeproj/ Package.resolved && vapor xcode -y and even git reset Package.resolved file but errors are still there. Now it seems that I canā€™t get the project right anymore :frowning:, please help!

Vapor Toolbox: 3.1.4
Vapor Framework: 3.0.0-rc.2.2
Xcode Version 9.2 (9C40b)
Toolchain: Swift 4.1 Snapshot 2018-03-11 (a)

@vinamelody what build error are you getting? If itā€™s to do with NIO or OpenSSL, make sure you have the latest version of the vapor toolbox (brew upgrade vapor) as RC2 requires LibreSSL now and upgrading the toolbox will pull that down

Thanks @0xtim for the tips. brew upgrade vapor solved the issues.
Now, Iā€™m getting new Swift compiler errors :sweat:

@vinamelody awesome, thatā€™s easier to fix! Those will be the changes required for RC2 - see this Gist for what you need to do, there should only be a few things you need

1 Like

@subhransu @vinamelody would help if I linked it! Have edited the post to add the Gist

After fixing the code errors, upon hitting /api/acronyms/1/categories/1, I get this response error:

{
    "error": true,
    "reason": "Table 'vapor.acronym_category' doesn't exist"
}

Both /api/acronyms and /api/categories work and return the right json

Also, tried doing a fresh database.

docker stop mysql
docker rmi <docker image id for mysql>
docker run --name mysql -e MYSQL_USER=til -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor -p 3306:3306 -d mysql/mysql-server
docker exec -it mysql mysql -u til -ppassword

Followed by swift package update with these responses:

Updating https://github.com/vapor/vapor.git
Updating https://github.com/vapor/fluent-mysql.git
Updating https://github.com/vapor/leaf.git
Updating https://github.com/vapor/template-kit.git
Updating https://github.com/vapor/service.git
Updating https://github.com/vapor/engine.git
Updating https://github.com/vapor/database-kit.git
Updating https://github.com/vapor/core.git
Updating https://github.com/vapor/console.git
Updating https://github.com/vapor/crypto.git
Updating https://github.com/vapor/routing.git
Updating https://github.com/vapor/validation.git
Updating https://github.com/apple/swift-nio.git
Updating https://github.com/apple/swift-nio-zlib-support.git
Updating https://github.com/apple/swift-nio-ssl.git
Updating https://github.com/apple/swift-nio-ssl-support.git
Updating https://github.com/vapor/mysql.git
Updating https://github.com/vapor/fluent.git
Resolving https://github.com/vapor/vapor.git at 3.0.0-rc.2.2.1
Resolving https://github.com/vapor/core.git at 3.0.0-rc.2.4
Resolving https://github.com/vapor/validation.git at 2.0.0-rc.2.1.1
Resolving https://github.com/vapor/mysql.git at 3.0.0-rc.2.1.2
Resolving https://github.com/vapor/console.git at 3.0.0-rc.2.2
Resolving https://github.com/vapor/leaf.git at 3.0.0-rc.2.1
Resolving https://github.com/vapor/fluent-mysql.git at 3.0.0-rc.2.3
Resolving https://github.com/vapor/fluent.git at 3.0.0-rc.2.3
Resolving https://github.com/vapor/template-kit.git at 1.0.0-rc.2.0.3

Followed by vapor xcode -y, and got this run fail:

[ INFO ] Migrating 'mysql' database (FluentProvider.swift:28)
[ INFO ] Preparing migration 'Acronym' (MigrationContainer.swift:50)
āš ļø [FluentError.reflectProperty: No property reflected for Swift.WritableKeyPath<App.Acronym, Swift.Optional<Swift.Int>>] [/Users/vinamelody/Code/vapor/tilapp/.build/checkouts/fluent.git-6251908308727715749/Sources/Fluent/Migration/Migration.swift:28:124]
Program ended with exit code: 1

Code here

@vinamelody so youā€™ve hit a couple of issues there! The first is that the table name logic was changed in a recent RC, which unhelpfully meant that existing tables didnā€™t match upā€¦

The current issue youā€™re seeing is to do with the new reflection stuff that went in last night. Unfortunately it seems to have broken models that are classes :angry:

There isnā€™t much you can do at the moment apart from wait until it has been fixed. Itā€™s all to do with how the layout of a class looks in memory and the fact that computed properties donā€™t have offsets, which has broken itā€¦ Iā€™ll post on here when it has fixed.

One final solution you have is to replace your Package.resolved with the following:

{
  "object": {
    "pins": [
      {
        "package": "Auth",
        "repositoryURL": "https://github.com/vapor/auth.git",
        "state": {
          "branch": null,
          "revision": "af15d9b23a421d76754535447b8cb791d9ce5fc6",
          "version": "2.0.0-rc.3"
        }
      },
      {
        "package": "Console",
        "repositoryURL": "https://github.com/vapor/console.git",
        "state": {
          "branch": null,
          "revision": "9b219b46ce0248924ed014c23bf8d4df05f2ad80",
          "version": "3.0.0-rc.2.0.1"
        }
      },
      {
        "package": "Core",
        "repositoryURL": "https://github.com/vapor/core.git",
        "state": {
          "branch": null,
          "revision": "92cef8935fbeb16516b3c687ca3039fa5ce60860",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "Crypto",
        "repositoryURL": "https://github.com/vapor/crypto.git",
        "state": {
          "branch": null,
          "revision": "5ea23edc6d3122655daf71756bc769c987a20b29",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "DatabaseKit",
        "repositoryURL": "https://github.com/vapor/database-kit.git",
        "state": {
          "branch": null,
          "revision": "965d4c8ce376819456e0bc1a6c8f388b4d6555cd",
          "version": "1.0.0-rc.2"
        }
      },
      {
        "package": "Engine",
        "repositoryURL": "https://github.com/vapor/engine.git",
        "state": {
          "branch": null,
          "revision": "6a7ca70b6858a0ac8a5c48359c80a3c37f46b47e",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "Fluent",
        "repositoryURL": "https://github.com/vapor/fluent.git",
        "state": {
          "branch": null,
          "revision": "9e684c00fc2bd1fd1adaaad5f6ae29beac7b0364",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "FluentMySQL",
        "repositoryURL": "https://github.com/vapor/fluent-mysql.git",
        "state": {
          "branch": null,
          "revision": "e941ebbb989a22a9dd15360681d7a511354d4ee7",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "Leaf",
        "repositoryURL": "https://github.com/vapor/leaf.git",
        "state": {
          "branch": null,
          "revision": "e0c8bb646556aefdaed907e48a8435655c59bab3",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "MySQL",
        "repositoryURL": "https://github.com/vapor/mysql.git",
        "state": {
          "branch": null,
          "revision": "4057d62f5576e305b6ff4b99fc3dd2eca24497a7",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "Routing",
        "repositoryURL": "https://github.com/vapor/routing.git",
        "state": {
          "branch": null,
          "revision": "2fc1d4de22a54848b35ad17b3e7f7816f19ebf90",
          "version": "3.0.0-rc.2"
        }
      },
      {
        "package": "Service",
        "repositoryURL": "https://github.com/vapor/service.git",
        "state": {
          "branch": null,
          "revision": "8ade63fc255a184ec16a1c66562b837f2b5db83f",
          "version": "1.0.0-rc.2"
        }
      },
      {
        "package": "swift-nio",
        "repositoryURL": "https://github.com/apple/swift-nio.git",
        "state": {
          "branch": null,
          "revision": "320561f4371d32c74a12158d2e18f220703fe32c",
          "version": "1.2.1"
        }
      },
      {
        "package": "swift-nio-ssl",
        "repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
        "state": {
          "branch": null,
          "revision": "85a55f91bf80afa96889426962f0e0369fae9187",
          "version": "1.0.0"
        }
      },
      {
        "package": "swift-nio-ssl-support",
        "repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git",
        "state": {
          "branch": null,
          "revision": "c02eec4e0e6d351cd092938cf44195a8e669f555",
          "version": "1.0.0"
        }
      },
      {
        "package": "swift-nio-zlib-support",
        "repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
        "state": {
          "branch": null,
          "revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
          "version": "1.0.0"
        }
      },
      {
        "package": "TemplateKit",
        "repositoryURL": "https://github.com/vapor/template-kit.git",
        "state": {
          "branch": null,
          "revision": "ef940383716d05ac6e1b0dcb0ba672c7c0f8a718",
          "version": "1.0.0-rc.2.0.1"
        }
      },
      {
        "package": "Validation",
        "repositoryURL": "https://github.com/vapor/validation.git",
        "state": {
          "branch": null,
          "revision": "361140562d87385df49fdb36e96841c0a92d59ae",
          "version": "2.0.0-rc.2"
        }
      },
      {
        "package": "Vapor",
        "repositoryURL": "https://github.com/vapor/vapor.git",
        "state": {
          "branch": null,
          "revision": "5f757a9a47c494af030d1ab7f82911e140682f9d",
          "version": "3.0.0-rc.2"
        }
      }
    ]
  },
  "version": 1
}

Then run rm -rf .build TIL.xcodeproj. That will remove everything and give you a working version set (I think). If you run vapor xcode -y that should use that file so you can keep going until the issue gets fixed.

Hmm, still having the same error ā€¦ you can inspect my code here GitHub - vinamelody/tilapp: Vapor server side ā€¦ oh my perhaps Iā€™ll just wait for the fix :frowning:

I think itā€™s now fixed - try running vapor update and it should work again

Did vapor update and getting another kind of error hereā€¦

Update: Fixed by doing rm -rf .build tilapp.xcodeproj and vapor xcode -y again ā€¦ oh boy feels like it is so dangerous to do vapor update at any point of time, esp in between development. Iā€™m not sure about the assertion failed that just happened.

Ah good old RCs - thereā€™s been a couple of frustrating changes the last couple of days before the release hits. However when in doubt do a clean and try a build again. vapor update should regenerate the project for you but sometimes Xcode is a bit keen on the cachingā€¦

1 Like

That assertion failed appeared again when Iā€™m about to test User Password feature. That is, by removing mysql docker, create new one and run the project again. Therefore, I came back here and did another vapor update

uhh different Build error this time ā€¦

:angry: more breaking changes as the releases roll out. Core 3.0.0 moved DirectoryConfig to another module, so you can either edit LeafProvider.swift directory and add an import Core to the top, or point your Leaf dependency to the DirectoryConfigFix. Iā€™ve got a PR here to fix it Directory config fix by 0xTim Ā· Pull Request #100 Ā· vapor/leaf Ā· GitHub

hmm alright will wait until your PR is approved :+1:

@vinamelody the PR has just been merged, run vapor update and the problem should be fixed

For some reason, the assertion failed still occur. I stopped it and re-run again and itā€™s starting properly.

1 Like