To create a composite foreign key on Postgres using Typeorm, you need to follow these steps:
@Entity()
export class Table1 {
@PrimaryGeneratedColumn()
id: number;
@Column()
column1: string;
@OneToMany(() => Table2, (table2) => table2.table1)
table2s: Table2[];
}
@Entity()
export class Table2 {
@PrimaryGeneratedColumn()
id: number;
@Column()
column2: string;
@ManyToOne(() => Table1, (table1) => table1.table2s)
@JoinColumn({ name: 'table1_id' })
table1: Table1;
@ManyToOne(() => AnotherTable, (anotherTable) => anotherTable.table2s)
@JoinColumn({ name: 'another_table_id' })
anotherTable: AnotherTable;
}
You can customize the name of the foreign key column by passing a name option to the @JoinColumn decorator.
@JoinColumn({ name: 'table1_id' })
You can also specify multiple foreign keys by using multiple @JoinColumn decorators on the same column.
@JoinColumn({ name: 'table1_id' })
@JoinColumn({ name: 'another_table_id' })
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-06 12:50:52 +0000
Seen: 11 times
Last updated: Jun 06 '23
Can the names of rows be modified in MUI DataGrid?
How to modify the name of existing captions in MS Word?
How can you use linq to choose a specific column from a datatable?
What is the meaning of the role being returned as 'undefined' in DiscordJS?
What is the process of obtaining a column name by using a given row and its corresponding value?
What is the process for changing the name of a dropdown in a Grafana table query?