And still managing to do @OneToMany in another entity.
export class ProductsOfOrder {
@ManyToOne(() => Order, order => order.products)
order: Order
@ManyToOne(() => Product)
product: Product
@Column({type: 'integer'})
amount: number
}
In the case using the foreign key of order
@Entity()
export class Order {
@PrimaryGeneratedColumn('uuid')
id: string
@ManyToOne(() => User)
user: User
@OneToMany(() => ProductsOfOrder, productsOfOrder => productsOfOrder.order, {cascade: true})
products: ProductsOfOrder[]
}
Ciao, no you can't because its required for entities to have a primary column in terms of ORM because most of ORM operations are heavily rely on entity primary ids.
Ahem, of course, you can create a table without a primary key in TypeORM.
Here's a nice dirty little hack:
@Entity()
export class EntityWithoutPK {
/**
* ! This is a fake attribute
* This is a workaround for TypeORM's `MissingPrimaryColumnError`
**/
@PrimaryColumn({ type: 'uuid', insert: false, select: false, update: false })
id: never;
// Other column definitions
}
Generate a migration file and then delete the primary column part from the generated SQL
Set synchronize option to false
@Entity({ synchronize: false })
export class Order
Notes
Before applying a hack, see the following checklist. If you have a "no" for any one of them, create a primary key.
synchronize: false option might cause lots of panic.For your case, it makes total sense to create a primary key in the order table.
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