Hi, I am starting with a project with TypeScript, TypeORM and Postgres. I made the entities that seems to be correct but I got some errors I do not understand. The problem found is between a relationship @ManyToOne – @One ToMany. Here are my declarations and the console error.
// 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[];
}
// Pourpose: 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[];`
}
And here is the error:
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.
src/categories/categories.entity.ts:22:20 – error TS2552: Cannot find name ‘Products’. Did you mean ‘Product’?
22 @OneToMany(() => Products)
~~~~~~~~
src/categories/categories.entity.ts:24:13 – error TS2552: Cannot find name ‘Products’. Did you mean ‘Product’?
24 products: Products[];
~~~~~~~~
src/categories/categories.entity.ts:24:13 – error TS4031: Public property ‘products’ of exported class has or is using private name ‘Products’.
24 products: Products[];
~~~~~~~~
[10:37:23] Found 4 errors. Watching for file changes.
All declarations are right, and I do not understand what happens. For me seems to be a bug on TypeScript, Pretier. I tried to add a second argument with an empty object {} and with {cascade: true}, but it didn’t work
Edmundo Kinast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.