Relations with multiple primary keys
See original GitHub issueI 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:
- Created 7 years ago
- Comments:6 (5 by maintainers)
I was talking about similar behavior in #43. The solution is to create unique index on your foreign keys and create
@PrimaryGeneratedColumn()
inPostCategoryRelation
The simplest solution with
@ManyToOne(_type => Post, { primary: true })
isn’t implemented yet.@19majkel94 yes, it does