我只是使用 TypeORM 并发现关系列未定义
@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
entity:
export class Organization {
@PrimaryGeneratedColumn('uuid')
id!: string;
...
}
当我使用Repository
like:
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,typeORM 如何做到这一点?外键存在未定义?