Skip to content

Select SQL query for the fluent-postgres-driver with bindingsΒ #214

@nikitakuzmin2017

Description

@nikitakuzmin2017

Hello everyone!
I do not understand how to put bindings properly in SQL query.
Always I just get 500 error.
I add them as 'PostgresData', then put after query SQL.
It looks like this:

// PostgreSQL
        var returnValue: [Song] = []
        
        guard let postgres = req.db as? PostgresDatabase else {
            throw Abort(.internalServerError)
        }
        
        let id = PostgresData(int32: 289) // add binding
        
        do {
            let results = try await postgres.query("SELECT songs FROM bangs WHERE songDuration = $1", [id]).get() // add binding here as an array, '$1' inside the array
            
            for result in results {
                returnValue.append(try result.sql().decode(model: Song.self))
            }
        } catch {
            throw Abort(.accepted) // error here!
        }
        return returnValue

Could you please help me, I am struggling with this for one week looking for any documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions