【问题标题】:TypeORM @OneToMany doesn't appear to try and execute when calledTypeORM @OneToMany 在调用时似乎没有尝试执行
【发布时间】:2021-03-16 00:07:40
【问题描述】:

我已经查看了这些示例,据我所知,我一直遵循它们...我的 @ManyToOne 连接按预期在查询中运行,但是当包含在我的 graphql 操场中时,@OneToMany 似乎没有触发呼叫.. 用户的所有其他数据都按预期返回,但设备信息除外

这是在我的 User.ts 实体中:

@Field(type => [Devices], { nullable: true })
@OneToMany(() => Devices, devices => devices.user)
devices: Devices[];

这是我的 Devices.ts 实体中的条目:

  Field(type => User)
  @ManyToOne(type => User, { lazy: true })
  @JoinColumn([{ name: "userId", referencedColumnName: "userId" }])
  user: User;

在我添加 nullable: true 之前,我只是收到一个错误,说 can't return null for required blah blah ...我想也许我有一个没有用户的设备,但事实并非如此。在终端查询输出它只查询用户表并且不尝试获取设备...

我的 graphql 调用非常简单,但只为所有设备返回空值:

{
  users{
    name
    phone
    devices{
      deviceId
    }
  }
}

在我设置 {lazy: true } 之前,我的 @ManyToOne 还为用户返回空值,我也不知道为什么会这样。@OneToMany 显然没有这样的设置。我是续集,javascript 宝贝和这是我对 TypeORM 的第一次破解,因此我们将不胜感激。或者如果有我没有输入的相关代码,请告诉我,我会做的..提前干杯

【问题讨论】:

    标签: typescript typeorm typegraphql


    【解决方案1】:

    有趣....我几乎没有在任何地方看到这样的例子,但我最终添加了 { lazy: true } 也可以拨打 @OneToMany 电话,它们现在可以工作了。我没有意识到这是一个选择

    @Field(type => [Devices], { nullable: true,  })
    @OneToMany(() => Devices, devices => devices.user, { lazy: true })
    devices: Devices[];
    

    【讨论】:

    猜你喜欢
    • 2018-08-28
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多