对象 JS 关系关联多对多 Returns 使用 withGraphFetched 清空

ObjectionJS Relation Assocation ManyToMany Returns Empty Using withGraphFetched

我在这里遗漏了一些东西,这让我抓狂。
这是一个使用 withGraphFetched 的非常简单的急切加载查询,我可以在我的控制台中看到相关查询正在正确执行,甚至在我的 SQL 编辑器中测试了查询并且它是正确的,但是反对实际上并没有分配对象的结果,我不知道为什么。

在以下示例中,响应对象上的 media 键始终为空数组。即使查询实际上 returns 记录。

奇怪的是,如果我从使用 withGraphFetched 更改为 withGraphJoined,它将 return 正确的数据,尽管只有一条记录。

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",
                },
            },
        };
    }
}

和查询

Article.query().withGraphFetched("media");

returns(注意空媒体[])

    {
      "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": []
    },

我在我的控制台上可以清楚地看到

  knex:query select "media".* from "media" where "media"."article_id" in (?, ?, ?, ?, ?, ?, ?, ?, ?) undefined

当 运行 直接在数据库上时,它显示正确的记录...

奇怪的是,当更改为

Article.query().withGraphJoined("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": [
        {
          "id": 18,
          "assetId": "xxxx"
        }
      ]
    },

所以在反对级别发生了一些事情,它没有附加记录并且由于某种原因 withGraphFetched 以一种我不理解的奇怪方式表现

如有任何帮助,我们将不胜感激!

万一其他人遇到这个令人困惑的问题,该项目的一位维护者为我解决了这个问题。答案

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

参考:https://github.com/Vincit/objection.js/issues/2038

我遇到了与问题相反的问题,即 withGraphFetched 一切正常,但当更改为 withGraphJoined 时,所有关系都是空对象。我终于找到了解释,因为在我找到它之前它让我抓狂,所以我 post 把它放在这里,以防有人遇到同样的问题。

我遇到的问题是由仍然打开的 Knex bug 引起的,如果您使用 connectionString/connectString 和 Postgres,它会使 columnInfo 失败。下面先是我有问题的配置,然后是固定的配置。

// 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,
});

如果您有需要的连接属性,那么一种选择是使用 pg-connection-string 包解析连接字符串,例如:

const { parse } = require("pg-connection-string");
const knex = Knex({
  client: "pg",
  connection: {
    ...parse(process.env.DATABASE_URL),
    ssl: { rejectUnauthorized: false },
  },
});