Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
Why does my Vapor query always fail?

I am using Vapor to write a simple web app. I have an entity to save session info into a database. The code for this entity is as follows:

struct SessionToken: Model{
    var exists: Bool = false
    var id: Node?
    var username: String
    var accessToken: String
    var expiryDate: String

    init(username: String, accessToken: String, expiryDate: String) {
        self.username = username
        self.accessToken = accessToken
        self.expiryDate = expiryDate
    }

    init(node: Node, in context: Context) throws {
        try self.id = node.extract("id")
        try self.username = node.extract("username")
        try self.accessToken = node.extract("accessToken")
        try self.expiryDate = node.extract("expiryDate")
    }


    func makeNode(context: Context) throws -> Node {
        let sessionToken = try Node(node:["id": id, "username": username, "accessToken": accessToken, "expiryDate": expiryDate])
        return sessionToken
    }


    static func prepare(_ database: Database) throws {
        try database.create("sessiontokens"){ sessiontokens in
            sessiontokens.id()
            sessiontokens.string("username")
            sessiontokens.string("accessToken")
            sessiontokens.string("expiryDate")

        }
    }

    static func revert(_ database: Database) throws {
        try database.delete("sessiontokens")
    }

}

I enter some fake info into the database, but when I query the database with the following code:

let token = try SessionToken.query().filter("username", "user@email.com").first()

I get an error: Could not initialize SessionToken, skipping: unableToConvert(node: nil, expected: "String")

The funny thing is that when I replace SessionToken with any other entity in the project the code works just fine. The only entity I am having trouble fetching any info from the database is SessionToken and it has been frustrating me since last night!

I would really appreciate it if someone could point me in the right direction to solve this problem.

P.S. I am using Postgres.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I found the source of the problem. As mentioned by the error Could not initialize SessionToken there is a problem with initialization of the entity. Postgres creates column names with all lower case letters so if you create an "accessToken" column, it is saved as "accesstoken" in the table without the capital T. As you can see I am querying the table with "accessToken" and no such column exists; therefore, the source of the problem was naming discrepancies between the table and the query.

You can read more about Postgres case sensitivity here: Are PostgreSQL column names case-sensitive?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda