TypeORM为什么我的关系列未定义?外键未定义 [英] TypeORM why is my relationship column undefined? foreign-key is undefined

查看:99
本文介绍了TypeORM为什么我的关系列未定义?外键未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是使用TypeORM并发现关系列未定义

I just use TypeORM and find the relationship column is undefined


@Entity({name: 'person'})
export class Person {
    @PrimaryGeneratedColumn('uuid')
    id!: string;

    @OneToOne( () => User)
    @JoinColumn()
    user!: User;

    @Column({
        type: "enum",
        enum: PersonTitle,
        default: PersonTitle.Blank
        })
    title?: string;

    @Column({type: 'varchar', default: ''})
    first_name!: string;

    @Column('varchar')
    last_name!: string;

    @ManyToOne(() => Organization, org => org.people, { nullable: true})
    belong_organization!: Organization;

,我也有 Organization 实体:

export class Organization {
    @PrimaryGeneratedColumn('uuid')
    id!: string;
...
}

当我使用 Repository 之类的时候,

 const db = await getDatabaseConnection()
 const prep = db.getRepository<Person>('person')
 presult = await prep.findOne({where: {id}})
 console.log(result)

我的结果是:

Person {
  id: '75c37eb9-1d88-4d0c-a927-1f9e3d909aef',
  user: undefined,
  title: 'Mr.',
  first_name: 'ss',
  last_name: 'ls',
  belong_organization: undefined,  // I just want to know why is undefined? even I can find in database the column
  expertise: [],
  introduction: 'input introduction',
  COVID_19: false,
  contact: undefined
}

数据库表如下:

"id"    "title" "first_name"    "last_name" "expertise" "COVID_19"  "userId"    "belongOrganizationId"  "introduction"
"75c37eb9-1d88-4d0c-a927-1f9e3d909aef"  "Mr."   "test"  "tester"    "nothing"   "0" "be426167-f471-4092-80dc-7aef67f13bac"  "8fc50c9e-b598-483e-a00b-1d401c1b3d61"  "input introduction"

我想显示组织ID,它是怎么做的?外键是否存在未定义?

I want to show organization id, how typeORM do it? Foreign-Key is present undefined?

推荐答案

您需要延迟加载关系,或者需要在查找中指定关系

You need to either lazy load the relation or you need to specify the relation in the find

惰性:

@Entity({name: 'person'})
class Person {
    ...
    @ManyToOne(() => Organization, org => org.people, { nullable: true})
    belong_organization!: Organization;
    ...
}

...

async logOrganization() {
    const db = await getDatabaseConnection()
    const prep = db.getRepository<Person>('person')
    presult = await prep.findOne({where: {id}})
    console.log(await result.belong_organization)
}

查找

const prep = db.getRepository<Person>('person')
presult = await prep.findOne({
    where: { id },
    relations: ["belong_organization"]
})

您也可以总是进行急切的加载,但是我建议您不要这样做,因为那样一来,当它获取一个人时,它将始终执行连接.

You could also always do an eager load, but i'd advise against this since then it would always do the join when it fetches a person.

如果要查询 belong_organizationId ,则需要将其字段添加到 person 实体.此字段通常类似于 belongOrganizationId

If you want to query the belong_organizationId you need to add its field to the person entity. This field is usual something like belongOrganizationId

那会

@Entity({name: 'person'})
class Person {
    ...
    @Column()
    belongOrganizationId:number

    @ManyToOne(() => Organization, org => org.people, { nullable: true})
    belong_organization!: Organization;
    ...
}

这也可以查询其ID.

您还可以更直接地查询它,但这会给您留下一些非常丑陋且难以维护的代码:

You could also query it more directly but this leaves you with some pretty ugly and unmaintainable code:

const findOptions: {
    where :{
        id,
        'belong_organization.id': belong_organizationId
    }
}

这篇关于TypeORM为什么我的关系列未定义?外键未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆