gpt4 book ai didi

typeorm-如何在 "many to many"表中添加额外的字段?

转载 作者:行者123 更新时间:2023-12-03 15:28:50 25 4
gpt4 key购买 nike

,例如:

3 table

user
user_business_lines_business_line
business_line

typeorm创建并带有 User中的声明的代码
@ManyToMany(type => BusinessLine)
@JoinTable()
businessLines: BusinessLine[]

然后,如何添加列字段
@CreateDateColumn({ type: 'timestamp' })
createdAt: Date

@UpdateDateColumn({ type: 'timestamp' })
updatedAt: Date

user_business_lines_business_line

最佳答案

无法在自动创建的多对多桥接表中添加自定义列。因此,创建另一个表,并在它们之间建立一对多和多对一的关系。
,例如:
三 table
用户->表1
业务线->表2
UserBusinessLine -> User表和BusinessLine表之间的桥接表

UserBusinessLine table will contain the foreign key of both parent tables and also we can add custom culomns into it.
在用户表中
@OneToMany(() => UserBusinessLine, (userBusinessLine) => userBusinessLine.user)
public userBusinessLines: UserBusinessLine[];
在BusinessLine表中
@OneToMany(() => UserBusinessLine, (userBusinessLine) => userBusinessLine.businessLine)
public userBusinessLines: UserBusinessLine[];
在UserBusinessLine表中
@ManyToOne(() => User, (user) => user.userBusinessLines)
public user: User;

@ManyToOne(() => User, (businessLine) => businessLine.userBusinessLines)
public businessLine: BusinessLine;

// Custom Colums
@CreateDateColumn({ type: 'timestamp' })
createdAt: Date;

@UpdateDateColumn({ type: 'timestamp' })
updatedAt: Date;
因此,现在定制表具有User表和BusinessLine表的外键。也是CreateddateColumn和UpdatedDateColumn

关于typeorm-如何在 "many to many"表中添加额外的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55253563/

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