Hello there! I recently embarked on a new project utilizing TypeScript, TypeORM, and Postgres. Everything seemed to be going smoothly until I encountered some perplexing errors related to a relationship between @ManyToOne and @OneToMany. Below are my entity declarations along with the console error that I'm facing:
// Entity for products table
import { Category } from './categories/categories.entity';
import { OrderDetails } from 'src/orders/details/orderDetails.entity';
import {
Column,
Entity,
JoinTable,
ManyToMany,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity({name: 'products`})
export class Product {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ length: 50, nullable: false })
name: string;
@Column({ nullable: false })
description: string;
@Column('decimal', { precision: 10, scale: 2, nullable: false })
price: number;
@Column({ nullable: false })
stock: number;
@Column({ nullable: true, default: 'default_image_url' })
imgUrl: string;
@ManyToOne(() => Category, (category) => category.products)
@JoinTable()
category: Category;
@ManyToMany(() => OrderDetails)
@JoinTable()
orderDetails: OrderDetails[];
}
// Purpose: Entity for categories table
import { Product } from '../products.entity';
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ name: 'categories' })
export class Category {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ length: 50, nullable: false })
name: string;
@OneToMany(() => Product, (product) => product.category)
products: Product[];
}
Here is the specific error message I am encountering: src/categories/categories.entity.ts:22:4 - error TS2554: Expected 2-3 arguments, but got 1. 22 @OneToMany(() => Products) ~~~~~~~~~ node_modules/typeorm/decorator/relations/OneToMany.d.ts:8:102 8 export declare function OneToMany(typeFunctionOrTarget: string | ((type?: any) => ObjectType), inverseSide: string | ((object: T) => any), options?: RelationOptions): PropertyDecorator; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ An argument for 'inverseSide' was not provided. ... [10:37:23] Found 4 errors. Watching for file changes.
I've double-checked my declarations and they appear to be correct. However, I'm at a loss as to why this error is occurring. It almost seems like an issue with TypeScript or Prettier. I even tried adding a second argument with an empty object {} or {cascade: true}, but unfortunately, it didn't resolve the problem.