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.

Relations with multiple primary keys

See original GitHub issue

I want to model a relation of two entities where the relation has some properties (eg. a Post has a many-to-many relation with Categories, but since the relation has some properties - like order or categerizedAt or whatever property you can imagine for such a relation - I have to introduce a join table I guess. I have tried to get the correct mapping with typeorm but failed:

  • how to define the primary key in the jointable?
  • persisting fails with [ERROR] [default] - Error: Entity PostCategoryRelation has multiple primary keys. This operation is not supported on entities with multiple primary keys at EntityMetadata.firstPrimaryColumn

First try:

@Table()
export class Post {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column()
    text: string;

    @OneToMany(type => PostCategoryRelation, postCategoryRelation => postCategoryRelation.post)
    categories: PostCategoryRelation[];
}

@Table()
export class PostCategory {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @OneToMany(type => PostCategoryRelation, postCategoryRelation => postCategoryRelation.category)
    posts: PostCategoryRelation[];
}

@Table()
export class PostCategoryRelation {
  @ManyToOne(type => Post, post => post.categories)
  @PrimaryColumn()
  post: Post;

  @ManyToOne(type => PostCategory, category => category.posts)
  @PrimaryColumn()
  category: PostCategory;

  @Column()
  addedByAdmin: boolean;

  @Column()
  addedByUser: boolean;
}

Did not work since PrimaryKey type cannot be determined. Next try:

@Table()
export class Post {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column()
  text: string;

  @OneToMany(type => PostCategoryRelation, postCategoryRelation => postCategoryRelation.post)
  categories: PostCategoryRelation[];
}

@Table()
export class PostCategory {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(type => PostCategoryRelation, postCategoryRelation => postCategoryRelation.category)
  posts: PostCategoryRelation[];
}

@Table()
export class PostCategoryRelation {

  @PrimaryColumn('int', { name: 'id_post' })
  private postRef: number;

  @PrimaryColumn('int', { name: 'id_cat' })
  private catRef: number;

  @ManyToOne(type => Post, post => post.categories)
  @JoinColumn({ name: 'id_post', referencedColumnName: 'id' })
  post: Post;

  @ManyToOne(type => PostCategory, category => category.posts)
  @JoinColumn({ name: 'id_cat', referencedColumnName: 'id' })
  category: PostCategory;

  @Column()
  addedByAdmin: boolean;

  @Column()
  addedByUser: boolean;
}

Now I get the above mentioned [ERROR] [default] - Error: Entity PostCategoryRelation has multiple primary keys. This operation is not supported on entities with multiple primary keys at EntityMetadata.firstPrimaryColumn

What is the right way to model this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
MichalLytekcommented, Oct 21, 2016

I was talking about similar behavior in #43. The solution is to create unique index on your foreign keys and create @PrimaryGeneratedColumn() in PostCategoryRelation

@Table()
@Index((relation: PostCategoryRelation) => [relation.post, relation.category], { unique: true })
export class PostCategoryRelation {
  @PrimaryGeneratedColumn()
  public readonly id: number;

  @ManyToOne(type => Post, post => post.categories)
  post: Post;

  @ManyToOne(type => PostCategory, category => category.posts)
  category: PostCategory;

  @Column()
  addedByAdmin: boolean;

  @Column()
  addedByUser: boolean;
}

The simplest solution with @ManyToOne(_type => Post, { primary: true }) isn’t implemented yet.

2reactions
pleerockcommented, Nov 29, 2016

@19majkel94 yes, it does

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a table have multiple Primary keys? - Stack Overflow
No.You cannot use more than 1 primary key in the table.for that you have composite key which is ...
Read more >
Defining Composite Primary and Foreign Keys - IBM
A referential constraint must have a one-to-one relationship between referencing and referenced columns. In other words, if the primary key is a set...
Read more >
Can Multiple Primary Keys Exist on a Single Table? - Chartio
A table is not allowed to contain multiple primary keys, as that goes against the fundamental principles of relational database design.
Read more >
Can there be two primary keys in the same table? - Quora
A table can have only one primary key. You can have primary key as a combination of two column, in that case its...
Read more >
Add or change a table's primary key in Access
Composite keys: using multiple fields in combination as a primary key ... In some cases, you want to use two or more fields...
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