$relatedQuery 方法不返回模态实体

Posted

技术标签:

【中文标题】$relatedQuery 方法不返回模态实体【英文标题】:$relatedQuery method doesn't return modal entity 【发布时间】:2018-02-21 20:17:26 【问题描述】:

我将 PostgreSQL 和 objection.js 用于 ORM。当我使用$relatedQuery 方法时,我得到QueryBuilder 对象而不是模型实体。

Here 是示例项目以及如何使用$relatedQuery 方法。

podcastListener.js

const  Model, snakeCaseMappers  = require('objection');

class Podcast extends Model 
  static get tableName() 
    return 'podcasts_listeners';
  

  static get columnNameMappers() 
    return snakeCaseMappers();
  

  static get jsonSchema() 
    return 
      type: 'object',

      properties: 
        id:  type: 'integer' ,
        name:  type: 'string', minLength: 1, maxLength: 100 ,
      ,
    ;
  

  static get relationMappings() 
    return 
      usersRel: 
        relation: Model.BelongsToOneRelation,
        modelClass: `$__dirname/user`,
        join: 
          from: 'podcasts_listeners.user_id',
          to: 'users.id',
        ,
      ,

      episodesRel: 
        relation: Model.HasManyRelation,
        modelClass: `$__dirname/episode`,
        join: 
          from: 'podcasts_listeners.id',
          to: 'episodes.podcasts_listeners_id',
        ,
      ,
    ;
  


module.exports = Podcast;

episode.js

const  Model, snakeCaseMappers  = require('objection');

class Episode extends Model 
  static get tableName() 
    return 'episodes';
  

  static get columnNameMappers() 
    return snakeCaseMappers();
  

  static get jsonSchema() 
    return 
      type: 'object',

      properties: 
        podcasts_listeners_id:  type: 'integer' ,
        id:  type: 'integer' ,
        episode_name:  type: 'string', minLength: 1, maxLength: 100 , ,
        description:  type: 'string' ,
      ,
    ;
  

  static get relationMappings() 
    return 
      podcastRel: 
        relation: Model.BelongsToOneRelation,
        modelClass: `$__dirname/podcastListener`,
        join: 
          from: 'episodes.podcasts_listeners_id',
          to: 'podcasts_listeners.id',
        ,
      ,
    ;
  


module.exports = Episode;

如果我跑:

const podcast = await PodcastListener.query()
    .findOne('name', podcastName);

  const episode = podcast
    .$relatedQuery('episodesRel')
    .findById(6);

或:

  const episode = podcast
    .$relatedQuery('episodesRel');

情节变量是QueryBuilder 而不是Episode 模型。

【问题讨论】:

【参考方案1】:

我错过了await

const episode = await podcast
    .$relatedQuery('episodesRel');

【讨论】:

以上是关于$relatedQuery 方法不返回模态实体的主要内容,如果未能解决你的问题,请参考以下文章

使用与模态视图冲突的实体卡

Syncretic Modality Collaborative Learning for Visible Infrared Person Re-Identification—建立融合模态,三模态共享

将实体绑定到模态

Qt 之 模态非模态半模态窗口的介绍及 实现QDialog的exec()方法

Spring Data Jpa:保存方法仅返回选择,但不执行插入

将 TextField 条目从模态视图控制器保存到核心数据单独的实体