Cătălin Avasiloaie
Cătălin Avasiloaie

Reputation: 196

TypeORM OneToMany creates foreign key column on another entity

I have three tables: User, UserToken and UserRank. The relationships between them are:

The behaviour I want is:

The problem is that I've added that UserRank OneToMany relationship and now I have the userRankId also in the UserTokens table, and I don't want this. Can you, please, help me solve this?

Thank you!

Upvotes: 0

Views: 2161

Answers (1)

Marcellia
Marcellia

Reputation: 212

You can define foreign key constraint with createForeignKeyConstraints option (default: true), you can set it to "false".

import { Entity, PrimaryColumn, Column, ManyToOne } from "typeorm"
import { Person } from "./Person"

@Entity()
export class ActionLog {
    @PrimaryColumn()
    id: number

    @Column()
    date: Date

    @Column()
    action: string

    @ManyToOne((type) => Person, {
        createForeignKeyConstraints: false,
    })
    person: Person
}
 

Upvotes: 1

Related Questions