gpt4 book ai didi

typescript - 我可以使用部分实体通过 typeorm 进行保存吗?

转载 作者:行者123 更新时间:2023-12-02 01:47:06 31 4
gpt4 key购买 nike

所以我有这个表结构

--changeset 0004-order:ccushing
create table if not exists "order"."order"
(
id uuid primary key not null default uuid_generate_v4(),
state uuid references "order".order_status
);

--changeset 0004-h0-table-order_event_type:ccushing
create table if not exists "order".order_event_type
(
id uuid primary key not null default uuid_generate_v4(),
key text unique not null
);

--changeset 0004-h1-table-order_event:ccushing
create table if not exists "order".order_event
(
id uuid primary key not null default uuid_generate_v4(),
order_id uuid not null references "order"."order" (id),
event_type uuid not null references "order".order_event_type (id),
event jsonb not null,
unique (order_id, event),
unique (order_id, event_type)
);

我想创建一个新的 OrderEventEntity,但我不想在执行此操作时加载 Order,因为我只会获取 order_id 在事件中。

@Entity('order.order_event')
export default class OrderEventEntity implements Identifiable<string> {
@PrimaryGeneratedColumn({ type: 'uuid' })
readonly id!: string;

@ManyToOne(() => OrderEventTypeEntity, ({ event }) => event)
readonly eventType!: string;

@ManyToOne(() => OrderEntity, ({ events }) => events)
readonly order!: OrderEntity;
}

我能做到吗

const order = new Order({ id: 1234 })
repo.save( new OrderEventEntity({ order: order, ... })

或类似的(也许是部分负载)而不丢失OneToMany?但仍然只有订单 ID。

最佳答案

有两个选项:

  1. OrderEventEntity 中定义 order_id
@Entity('order.order_event')
export default class OrderEventEntity implements Identifiable<string> {
@PrimaryGeneratedColumn({ type: 'uuid' })
readonly id!: string;

@ManyToOne(() => OrderEventTypeEntity, ({ event }) => event)
readonly eventType!: string;

@Column()
order_id: string;

@ManyToOne(() => OrderEntity, ({ events }) => events)
readonly order!: OrderEntity;
}
  • 使用武力施展诡计
  • const order = { id: 1234 } as Order;
    repo.save( new OrderEventEntity({ order: order, ... })

    关于typescript - 我可以使用部分实体通过 typeorm 进行保存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60271852/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com