1

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!

Cheyne
  • 1,964
  • 4
  • 27
  • 43

2 Answers2

3

Incase anyone else comes across this confusing issue, the problem was resolved by one of the maintainers of the project for me. The answer

You are using knexSnakeCaseMappers, but not declaring things in camel case

Ref: https://github.com/Vincit/objection.js/issues/2038

Cheyne
  • 1,964
  • 4
  • 27
  • 43
0

I had an opposite issue than in the question, i.e. withGraphFetched everything worked nicely but when changed to withGraphJoined all the relations were empty objects. I finally found the explanation and since it was driving me nuts before I found it I post it here in case someone lands here with the same issue.

The issue I had was caused by the still-open Knex bug which makes columnInfo fail if you use connectionString/connectString and Postgres. Below is first my problematic configuration and then the fixed configuration.

// withGraphJoined doesn't work
const knex = Knex({
  client: "pg",
  connection: {
    connectionString: process.env.DATABASE_URL,
  },
});

// withGraphJoined works
const knex = Knex({
  client: "pg",
  connection: process.env.DATABASE_URL,
});

If you have connection properties you need then one option is to parse the connection string using pg-connection-string package, e.g.:

const { parse } = require("pg-connection-string");
const knex = Knex({
  client: "pg",
  connection: {
    ...parse(process.env.DATABASE_URL),
    ssl: { rejectUnauthorized: false },
  },
});
Samuli Asmala
  • 1,755
  • 18
  • 24