Autquery not including nested result when using a response DTO

Viewed 23

Let's say you have these models:

public class Blog
{
    [PrimaryKey]
    [AutoIncrement]
    public int Id { get; set; }
    public string Url { get; set; }
    public string PrivateField { get; set; }
    [Reference]
    public List<BlogToBlogCategory> BlogToBlogCategories { get; set; }
}

public class BlogResponse
{
    public int Id { get; set; }
    public string Url { get; set; }
    public List<BlogToBlogCategory> BlogToBlogCategories { get; set; }
}

And this request:

public class BlogsLookUpRequest : QueryDb<Blog, BlogResponse>
{
    
}

The return value will have BlogToBlogCategories as null, but this request:

public class BlogsLookUpRequest : QueryDb<Blog>
{
    
}

Will have BlogToBlogCategories populated. I can manually create the query response like so with custom implementation:

var q = _autoQuery.CreateQuery(request, Request.GetRequestParams(), base.Request);
var results = _autoQuery.Execute(request,q, base.Request);
return new QueryResponse<ResponseBlog>()
{
    Results = results.Results.ConvertTo<List<ResponseBlog>>(),
    Offset = request.Skip,
    Total = results.Total
};

Then it will have the nested results. If I decorate the collection with [Reference] then it is trying to find foreign key on non-existant BlogResponse table.

Why are referenced results removed when specifying a return model with AutoQuery? Is there a way to mark it up so it works?

1 Answers

The POCO Reference Types is used to populate Data Models not adhoc Response DTOs.

In this case it's trying to resolve references on a non-existent table, you can specify which table the DTO maps to with [Alias] attribute, e.g:

[Alias(nameof(Blog))]
public class BlogResponse
{
    public int Id { get; set; }
    public string Url { get; set; }
    public List<BlogToBlogCategory> BlogToBlogCategories { get; set; }
}
Related