Does typeorm support using parent entity's composite primary keys as part of child entity's composite primary key in foreign key relation?
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
Parent entity in many-to-one relationship:
@Entity('users')
export class User extends BaseEntity {
@PrimaryColumn('uuid')
id: string
@OneToMany(() => Device, device => device.user, { cascade: true })
devices: Device[]
}
Child uses foreign key to parent as part of it’s own composite primary key:
@Entity('devices')
export class Device extends BaseEntity {
@PrimaryColumn()
id: string
@Column()
label: string
@ManyToOne(() => User, user => user.devices, { primary: true })
user: User
@OneToMany(() => Thing, thing => thing.device, { cascade: true })
things: Thing[]
}
Problem arises when I try taking this pattern one level deeper by using the Device entity’s composite primary key as part of the Thing entity’s composite primary key:
@Entity('things')
export class Thing {
@PrimaryColumn()
address: string
@ManyToOne(() => Device, device => device.things, { primary: true })
@JoinColumn([
{ name: 'deviceId', referencedColumnName: 'id' },
{ name: 'userId', referencedColumnName: 'userId' },
])
device: Device
@Column()
symbol: string
}
What am I doing wrong here? I found this todo
in the source code, it sounds like it might be relevant and might be a missing feature if so.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:5 (2 by maintainers)
To clarify, the goal here is to make
Things
unique perDevice
andDevices
should be unique perUser
.Yes. This is definitely supported. See the tests in https://github.com/typeorm/typeorm/pull/6417 for examples