问题描述
在 MikroORM 中,如何通过原始数值设置外键?例如,在 Django ORM 中你可以这样做:
b = new Book()
b.author = someAuthorWithId1
还有这个,带有原始 ID
b = new Book()
b.author_id = 1
如何在 MikroORM 中使用原始 ID?
解决方法
有几种方法:
- 使用引用:
const b = new Book();
b.author = em.getReference(Author,1);
- 使用
assign
助手:
const b = new Book();
em.assign(b,{ author: 1 });
- 使用
create
助手:
const b = em.create(Book,{ author: 1 });