question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 issue

Issue 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:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
speakerwiggincommented, Oct 23, 2018

To clarify, the goal here is to make Things unique per Device and Devices should be unique per User.

0reactions
imnotjamescommented, Oct 5, 2020

Yes. This is definitely supported. See the tests in https://github.com/typeorm/typeorm/pull/6417 for examples

Read more comments on GitHub >

github_iconTop Results From Across the Web

Composite and Foreign Keys as Primary Key
MikroORM supports composite keys of primitive data-types as well as foreign keys as primary keys. You can also use your composite key entities...
Read more >
Relations - typeorm - GitBook
Relations helps you to work with related entities easily. ... NULL" - specifies how foreign key should behave when referenced object is deleted....
Read more >
Foreign key constraint »FK_0e4022833a9efc062c01637e552 ...
So I tried to setup a foreign key reference to the graphNode entity. It seems my entity design is not correct.
Read more >
Composite and Foreign Keys as Primary Key - ORM
Doctrine ORM supports composite primary keys natively. Composite keys are a very powerful relational database concept and we took good care to make...
Read more >
Documentation: 15: 5.11. Table Partitioning
The table is partitioned into “ranges” defined by a key column or set of ... or primary key constraint on a partitioned table,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found