微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使 Mikro-ORM 关系字段可选

如何解决使 Mikro-ORM 关系字段可选

我正在使用 mikro-orm 生成 graphql 模式,我的一对一关系中的所有字段都需要返回。我如何让它们成为可选的,以便我的查询在字段返回 null 时不会抛出错误?这是我在 ticket.entity.ts 中定义关系的方式

@Field(() => Order,{ nullable: true })
  @OnetoOne(() => Order,null,{ nullable: true })
  public order?: Order;

在我生成的 ticket.schema.graphql 中,Order 对象返回:

type Order {
  id: ID!
  createdAt: DateTime!
  updatedAt: DateTime!
  archivedAt: DateTime
  client: String!
  soldDate: DateTime!
  type: String!
  ...
  ...
  ...
}

在我的 Order 实体中,所有字段都是可选的,生成sql 表也理解它们。

export class Order extends BaseEntity {
  @Field(() => String)
  @Property({ nullable: true })
  public client: string;

  @Field(() => Date)
  @Property({ columnType: "date",nullable: true })
  public soldDate: Date;

  @Field(() => String)
  @Property({ nullable: true })
  public type: string;

  ...
  ...
  ...

我的订单实体中没有与票证的一对一关系。票有订单,但订单不一定有票。我在文档中没有看到单向关系,所以我想我会把它放在这里,以防它与我的问题有关。

解决方法

我的字段需要在订单实体中可以为空。这是一个 @nestjs/graphql 相关问题,而不是 mikro-orm 相关问题。

export class Order extends BaseEntity {
  @Field(() => String,{ nullable: true} )
  @Property({ nullable: true })
  public client: string;

  @Field(() => Date,{ nullable: true} )
  @Property({ columnType: "date",nullable: true })
  public soldDate: Date;

  @Field(() => String,{ nullable: true} )
  @Property({ nullable: true })
  public type: string;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。