Skip to content

[PostgreSQL] Storing Arrays of Custom Codable Types as JSONB[] instead of JSONB? #587

@Bamakoo

Description

@Bamakoo

Hi,

Whilst building a personal, side project I wanted to store an Array of Custom Codable Types.

struct CustomType: Codable {}

the resulting code in the tables' migration made use of the Custom option:

.array(of: .custom(CustomType.self))

All Vapor migrations ran like a charm but whilst testing it using Postman I ran into the following error:

Column custom_type is of type JSONB but expression is of type JSONB[]

Spent hours scouring the web for more info couldn't find any, banging my head against the wall trying to figure out what I was doing wrong with the JSON.

Finally, I reached out to Gwynne of the Vapor Core Team. She suggested swapping :

.array(of: .custom(CustomType.self))

for

.sql(raw: "JSONB[]")

Unsurprisingly, it worked!

Thank you for everything you do for the community.

Metadata

Metadata

Assignees

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