While doing Ray Wenderlich tutorial "Server Side Swift with Vapor: Persisting Models" I tried to add one more parameter(param) to the class Acronyms.
import Vapor
final class Acronym: Model {
var id: Node?
var exists: Bool = false
var short: String
var long: String
var param: String
init(short: String, long: String, param: String) {
self.id = nil
self.short = short
self.long = long
self.param = param
}
init(node: Node, in context: Context) throws {
id = try node.extract("id")
short = try node.extract("short")
long = try node.extract("long")
param = try node.extract("param")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"short": short,
"long": long,
"param": param
])
}
static func prepare(_ database: Database) throws {
try database.create("acronyms") { users in
users.id()
users.string("short")
users.string("long")
users.string("param")
}
}
static func revert(_ database: Database) throws {
try database.delete("acronyms")
}
}
At first I run this code without one more parameter. And it works. But when i added one it fails.
Error: 500The operation couldn’t be completed. (PostgreSQL.DatabaseError error 1.)
My main.swift:
import Vapor
import VaporPostgreSQL
let drop = Droplet(
preparations: [Acronym.self],
providers: [VaporPostgreSQL.Provider.self]
)
drop.get("hello") { request in
return "Hello, world!"
}
drop.get("version") { req in
if let db = drop.database?.driver as? PostgreSQLDriver {
let version = try db.raw("SELECT version()")
return try JSON(node: version)
} else {
return "No db connection"
}
}
drop.get("test") { request in
var acronym = Acronym(short: "AFK", long: "Away From Keyboard", param: "One More Parametr")
try acronym.save()
return try JSON(node: Acronym.all().makeNode())
}
drop.run()