How to create a composite key based on foreign keys?
See original GitHub issueI’m trying to create the entities and their relationships like in the ERD above.
User entity
import {Entity, PrimaryColumn, Column, CreateDateColumn, UpdateDateColumn} from "typeorm"`
@Entity()
export class User {
@PrimaryColumn()
id: string;
@Column( { type: "string", unique: true } )
IGN: string;
@Column()
age: number;
@CreateDateColumn()
created_at: Date;
@UpdateDateColumn()
updated_at: Date;
}
Achievement entity
import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn} from "typeorm";
@Entity()
export class Achievement {
@PrimaryGeneratedColumn()
id: string;
@Column( { type: "text", unique: true } )
name: string;
@Column()
description: string;
@CreateDateColumn()
created_at: Date;
@UpdateDateColumn()
updated_at: Date;
}
I’m having hard time to make the user_achievement entity. Since it has foreign keys that need to be turned into composite keys. How can I accomplish this?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Defining Composite Primary and Foreign Keys - IBM
A composite key specifies multiple columns for a primary-key or foreign-key constraint. The next example creates two tables. The first table has a...
Read more >Foreign key relationship with composite primary keys in SQL ...
When you create Table2, don't make a primary key at all in the beginning. First create the foreign key of Table2.FileID with Table1.FileID....
Read more >Foreign keys in a composite primary key - TechTarget
Is it possible to use one of the attributes of a composite primary key as a foreign key?
Read more >SQL - Foreign Key - 1Keydata
A composite foreign key is a foreign key that consists of two or more columns. It is important to note that all the...
Read more >Defining Composite Primary and Foreign Keys
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I did the following to accomplish this:
UserAchievement
I’m only unsure now if this is the correct way to turn my foreign keys into composite keys.
And it seems you can also set primary option to true inside the relation options. This way you wouldn’t have to use the @PrimaryColumn decorator:
@ManyToOne( () => Author, (author) => author.id, { primary: true, }, )