2
votes

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()
3

3 Answers

4
votes

I assume you didn't revert the database. You changed the model's properties, so just write in terminal vapor run prepare --revert . That will revert your database and vapor will be able to create new parameter.

2
votes

Another case when you done this

vapor run prepare --revert

and the error is still there. You should to check table name that you create in your prepare method in your model.

 static func prepare(_ database: Database) throws {
    try database.create(entity) { users in
        ...
    }
}

entity is the name of the table, as far as Vapor/Fluent thinks. By default, it's the name of the model, with an -s on the end.

For example: if you create Car model you should to name your table "cars". So Car+s

 static var entity = "cars"

Another example: You have model Carwash that becomes the grammatically incorrect carwashs. So you should to name is carwashs or use entity like this.

 static var entity = "carwashes"
1
votes

I run into the exactly same error, in my prepare method:

public static func prepare(_ database: Database) throws {
    try database.create(self.entity) { tasks in
        tasks.id()
        tasks.string(Identifiers.title)
    }
}

I was using self to refer to my database, like this:

public static func prepare(_ database: Database) throws {
    try self.database.create(self.entity) { tasks in
        tasks.id()
        tasks.string(Identifiers.title)
    }
}

Apparently, you must access the Database instance passed in as parameter, rather than the static Database variable. Hope this will help someone.