【发布时间】:2019-08-05 20:13:48
【问题描述】:
我不想创建用于将关系加载到其中的属性(如所有示例所示)。我唯一需要的是拥有一个显式外键属性,以便迁移 能够在数据库中为其创建适当的约束。最接近我需要的装饰器是@RelationId,但它仍然需要存在关系类的属性。
为了清楚起见,让我们以文档中的示例为例:
@Entity()
export class Post {
@ManyToOne(type => Category)
category: Category;
@RelationId((post: Post) => post.category) // it still requires the presence of the `category` proeprty
categoryId: number;
}
我在这里不需要category 属性。我想拥有categoryId 属性并将其标记为外键 到Category.Id。它应该是这样的:
@Entity()
export class Post {
@ForeignKey((category: Category) => category.Id) // it's a foreign key to Category.Id
categoryId: number;
}
有可能吗?
【问题讨论】:
-
嘿,你有办法解决这个问题吗?
-
对不起,伙计,我在一年前改变了我的技术栈。我相信我当时没有找到解决办法。
-
是的,我根据 charles 解决方案找到了它。
标签: foreign-keys typeorm typescript-decorator