vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
211 stars 116 forks source link

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

Open Bamakoo opened 11 months ago

Bamakoo commented 11 months ago

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.