Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving and retrieving an array of arrays of a custom type is not possible. #713

Open
abbasmousavi opened this issue Dec 20, 2020 · 5 comments
Labels
bug Something isn't working

Comments

@abbasmousavi
Copy link
Contributor

Describe the bug

saving and retrieving an array of arrays of a custom type is not possible.

To Reproduce

I have a person struct like this

public struct Person: Codable {
    
    public let name: String
    public let id: String?
    public init(name: String, id: String? = nil) {
        self.name = name
        self.id = id
    }
}

and in my model I have a field like this

@Field(key: "teams")
    var teams: [[Person]]?

Expected behavior

I want to be able to save it in my Postgres database.
I have used
.field("teams", .array(of: .json))
and
.field("teams", .json)
and

.field("teams", .array(of:.array(of: .json)))

as the field definition in my migration but none of them work.

Environment

  • Vapor Framework version: 4.36.2
  • Vapor Toolbox version:
  • OS version: 11.1
@abbasmousavi abbasmousavi added the bug Something isn't working label Dec 20, 2020
@0xTim
Copy link
Member

0xTim commented Dec 20, 2020

I suspect you'll need .field("teams", .json) if you want it to work, but what errors are you getting?

@abbasmousavi
Copy link
Contributor Author

If I use .field("teams", .json) it ends up on line 37 ofpostgres-nio/Sources/PostgresNIO/Data/PostgresData+Array.swift with this fatal error:

fatalError("No array type for \(elementType)")

Fatal error: No array type for JSONB[]

@0xTim
Copy link
Member

0xTim commented Dec 20, 2020

Is that after the save or before save? It's it's after the save, what does the JSON look like in the database?

@abbasmousavi
Copy link
Contributor Author

It is before save. It does not save to database.

@abbasmousavi
Copy link
Contributor Author

abbasmousavi commented Dec 23, 2020

@0xTim if I wrap it in a class like

class TeamWrapper: Codable {
 let team: [[Person]]
}

It will work without any problem, but the unwrapped double array causes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants