I have the following data model:
type User record {|
readonly string id;
string name;
int age;
Post[] posts;
|};
type Post record {|
readonly string id;
string title;
string content;
User author;
|};
After generating the persist client, I need to retrieve the posts for a particular user. I tried the following:
function getPosts(string id) returns db:Post[]|persist:Error? {
db:UserWithRelations user = check dbClient->/users/id;
return user.posts;
}
Note:
dbis the module generated by persist command.
But this shows the following error:
incompatible types: expected 'Post[]|ballerina/persist:1.0.0:Error)?', found 'PostOptionalized[]?'
If I change the return type of the function to db:PostOptionalized[], this error goes away.
What is the reason for this error? Is there a way to retrieve the posts without optional fields?
You can define a custom record type based on what you need to retrieve and set as a target type as follows,