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.

How to create a composite key based on foreign keys?

See original GitHub issue
Screenshot at Aug 28 19-16-58

I’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:closed
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

45reactions
onkarjitcommented, Aug 28, 2019

I did the following to accomplish this:

UserAchievement

import {CreateDateColumn, Entity, ManyToOne, JoinColumn, PrimaryColumn} from "typeorm";
import { User } from "./User";
import { Achievement } from "./Achievement";

@Entity()
export class UserAchievement {
    @PrimaryColumn()
    user_id: string;

    @PrimaryColumn()
    achievement_id: number;

    @ManyToOne(type => User, user => user.id)
    @JoinColumn({ name: "user_id" })
    public user!: User;

    @ManyToOne(type => Achievement, achievement => achievement.id)
    @JoinColumn({ name: "achievement_id" })
    public achievement!: Achievement;

    @CreateDateColumn()
    created_at: Date;
}

I’m only unsure now if this is the correct way to turn my foreign keys into composite keys.

26reactions
tareqdayyacommented, May 2, 2020

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, }, )

Read more comments on GitHub >

github_iconTop 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 >

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