Skip to content

Insufficient Data Error when Inserting UInt8 #42

@calebkleveter

Description

@calebkleveter

When trying to insert a new row into a table that is represented by a model with a UInt8 field, NIOPostgres throws an insufficient data left in message error.

I've created an MVP to display this behavior: https://github.com/calebkleveter/PostgresNIO-InsufficentData

  1. Clone the repo
  2. Configure a local PostgreSQL database. You can either customize the app configuration using environment variables or use the following values for the database:
    • Host: localhost
    • Port: 5432
    • User: Your current username
    • Password: password
    • Database Name: postgres-test
  3. Boot the application
  4. Run the POST /insert route.

You'll start off by getting an error that there was invalid JSON input:


[ ERROR ] Error(fields: [PostgresNIO.PostgresMessage.Error.Field.file: "json.c", PostgresNIO.PostgresMessage.Error.Field.routine: "report_invalid_token", PostgresNIO.PostgresMessage.Error.Field.detail: "Token \"\u{01}\" is invalid.", PostgresNIO.PostgresMessage.Error.Field.locationContext: "JSON data, line 1: \u{01}", PostgresNIO.PostgresMessage.Error.Field.severity: "ERROR", PostgresNIO.PostgresMessage.Error.Field.localizedSeverity: "ERROR", PostgresNIO.PostgresMessage.Error.Field.sqlState: "22P02", PostgresNIO.PostgresMessage.Error.Field.message: "invalid input syntax for type json", PostgresNIO.PostgresMessage.Error.Field.line: "1248"]) (PostgresNIO/Connection/PostgresRequest.swift:56)
[ ERROR ] NIOPostgres error: server error: invalid input syntax for type json (report_invalid_token) ["uuid": 548F078E-0996-4F1A-90D2-D656FAECA306] (Vapor/Middleware/ErrorMiddleware.swift:21)

This is because Fluent doesn't properly select the column type for a UInt8 field. You can fix this issue by manually setting the data type of the User.value field to .uint8:

struct User: Model {
    static var shared: User { User() }

    var id: Field<UUID?> = "id"
    var value: Field<UInt8> = Field("value", dataType: .uint8)
}

Once that is fixed, hit the POST /insert route again. This time you will get the insufficient data error that I mentioned before:

[ ERROR ] Error(fields: [PostgresNIO.PostgresMessage.Error.Field.line: "535", PostgresNIO.PostgresMessage.Error.Field.file: "pqformat.c", PostgresNIO.PostgresMessage.Error.Field.localizedSeverity: "ERROR", PostgresNIO.PostgresMessage.Error.Field.sqlState: "08P01", PostgresNIO.PostgresMessage.Error.Field.routine: "pq_copymsgbytes", PostgresNIO.PostgresMessage.Error.Field.message: "insufficient data left in message", PostgresNIO.PostgresMessage.Error.Field.severity: "ERROR"]) (PostgresNIO/Connection/PostgresRequest.swift:56)
[ ERROR ] NIOPostgres error: server error: insufficient data left in message (pq_copymsgbytes) ["uuid": 0E8389EE-7FD8-4A7E-95EB-24F70D6C875C] (Vapor/Middleware/ErrorMiddleware.swift:21)

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