I don't want to create a property for loading relation into it (as shown in all the examples). The only thing I need is to have an explicit foreign key property so that the migration will be able to create appropriate constraints for it in the database. The closest decorator to the one I need is @RelationId but it still requires the presence of a property of the relational class.
For clarity let's take the example from the documentation:
@Entity()
export class Post {
@ManyToOne(type => Category)
category: Category;
@RelationId((post: Post) => post.category) // it still requires the presence of the `category` proeprty
categoryId: number;
}
I don't need the category
property here. I want to have the categoryId
property and mark it as foreign key to Category.Id
. It should look like this:
@Entity()
export class Post {
@ForeignKey((category: Category) => category.Id) // it's a foreign key to Category.Id
categoryId: number;
}
Is it possible?
"I need is to have an explicit foreign key property"...
No, you could not. TypeOrm will automatically create foreign key property when you use @ManyToOne decorator. Just combine @ManyToOne and @JoinColumn decorators together like this:
@ManyToOne(type => Category)
@JoinColumn({ name: 'custom_field_name_if_you_want' })
category: Category;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With