如果模型没有 `id` 列 (但 `withGraphFetched` 仍然有效),则 `withGraphJoined` 将不起作用

作者: salisbury-espinosa创建于 2025年3月11日更新于 2025年3月11日
typescript
class First extends Model {
  static get tableName() {
    return 'first' as const;
  }

  static get idColumn(): never {
    throw new Error(`undefined id for first table`);
  }

  static get relationMappings() {
    return {
      second: {
        relation: this.HasOneRelation,
        modelClass: Second,
        join: {
          from: `${this.tableName}.secondId`,
          to: `${Second.tableName}.id`,
        },
      },
    };
  }
}

Failed behavior:

typescript
await First.query().withGraphJoined('second')

=> panic error undefined id for first table

Success behavior (replace withGraphJoined with withGraphFetched):

typescript
await First.query().withGraphFetched('second')

=> success!

内容来源: Vincit/objection.js