Skip to content

Warning given when saving optional containing nil field. #74

@Mladen-K

Description

@Mladen-K

Trying to save a model containing field with a nil value gives Warning:

[ INFO ] GET /health ["request-id": 4F2F36E9-26A1-4F39-9F53-B0AA8757EA81] (Vapor/HTTP/ApplicationResponder.swift:44)
[ INFO ] create test fields=[id, team, name] input=[[F1AEADD2-DFE8-47C1-BAFD-8E208E983CAC, nil, "NameWorks"]] ["request-id": 4F2F36E9-26A1-4F39-9F53-B0AA8757EA81] (FluentKit/Query/QueryBuilder.swift:702)
[ DEBUG ] No available connections on this event loop, creating a new one ["request-id": 4F2F36E9-26A1-4F39-9F53-B0AA8757EA81] (AsyncKit/ConnectionPool/EventLoopConnectionPool.swift:193)
[ DEBUG ] Logging into Postgres db xyz as xyz (PostgresNIO/Connection/PostgresConnection+Authenticate.swift:42)
[ DEBUG ] INSERT INTO "test" ("id", "team", "name") VALUES ($1, $2, $3) RETURNING id as "fluentID" [F1AEADD2-DFE8-47C1-BAFD-8E208E983CAC, <null>, "SomeName"] ["request-id": 4F2F36E9-26A1-4F39-9F53-B0AA8757EA81] (PostgresNIO/Connection/PostgresClient+Query.swift:39)
[ WARNING ] bind $2 type (UNKNOWN 0) does not match expected parameter type (TEXT) ["request-id": 4F2F36E9-26A1-4F39-9F53-B0AA8757EA81] (PostgresNIO/Connection/PostgresClient+Query.swift:74)
final class SaveError: Model, Content {
    static let schema = "test"
    
    @ID(key: "id")
    var id: UUID?

    @Field(key: "name")
    var name: String
    
    @Field(key: "team")
    var team: String?

    init() {}

    init(id: UUID? = nil, name: String, team: String? = nil) {
        self.id = id
        self.name = name
        self.team = team
    }
}


struct CreateSaveError: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("test")
            .field("id", .uuid, .identifier(auto: false))
            .field("name", .string, .required)
            .field("team", .string)
            .create()
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("test").delete()
    }
}

As @calebkleveter suggested:
line: https://github.com/vapor/postgres-nio/blob/master/Sources/PostgresNIO/Data/PostgresData%2BOptional.swift#L15
has to be:

return PostgresData(type: Wrapped.postgresDataType)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions