Im missing something here, and its driving me nuts.
This a very simple eager loaded query using withGraphFetched
, I can see the related query being performed correctly in my console, even tested the query in my SQL editor and its correct, however Objection is not actually assigning the results to the object and i have no idea why.
In the following example, the media
key on the response object is always an empty array. Even though the query actually returns records.
Strangely enough, if I change from using withGraphFetched
to withGraphJoined
it will return the right data although only a single record.
export default class Article extends Model implements IArticle {
static tableName = TABLES.ARTICLES;
static get relationMappings() {
return {
media: {
relation: Model.HasManyRelation,
modelClass: Media,
join: {
from: "articles.id",
to: "media.article_id",
},
}
}
}
export default class Media extends Model implements IMedia {
static tableName = TABLES.MEDIA;
static get relationMappings() {
return {
article: {
relation: Model.BelongsToOneRelation,
modelClass: Article,
join: {
from: "media.article_id",
to: "articles.id",
},
},
};
}
}
and the query
Article.query().withGraphFetched("media");
returns (note the empty media[])
{
"id": 1,
"slug": "nsw-flooding",
"title": "NSW Flooding",
"description": "Thousands evacuated from low lying areas across Sydney and NSW, with warnings of flood peaks not seen since 1922.",
"media": []
},
I can clearly see in my console
knex:query select "media".* from "media" where "media"."article_id" in (?, ?, ?, ?, ?, ?, ?, ?, ?) undefined
Which when run directly on the DB, it shows the correct records...
Oddly, when changing to
Article.query().withGraphJoined("media");
It works, although only shows one record
{
"id": 1,
"slug": "nsw-flooding",
"title": "NSW Flooding",
"description": "Thousands evacuated from low lying areas across Sydney and NSW, with warnings of flood peaks not seen since 1922.",
"media": [
{
"id": 18,
"assetId": "xxxx"
}
]
},
So something is happening at the objection level where its not attaching the records and for some reason withGraphFetched
behaves in a strange way that im not understanding
Any help would be greatly appreciated!