Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM Entity Metadata not found

ho,

I'm trying to build a backend for a type of e-commerce site using NestJS + TypeORM.

I need a m:n relation between my order and the products table. However, since I need some custom fields within the pivot table I looked up typeorms documentation and as stated there I need to create a new entity which I've done. Now I get an error starting up my NestJS application:

Error: Entity metadata for Product#productToOrders was not found. Check if you specified a correct entity object and if it's connected in the connection options.

product.entity.ts:

import { BaseEntity, Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { ProductToOrder } from "./productToOrder.entity";

@Entity()
export class Product extends BaseEntity{
    @PrimaryGeneratedColumn()
    id: number;
    
    @Column()
    name: string;
    
    @Column()
    description: string;
    
    @Column()
    price: number;

    @OneToMany(type => ProductToOrder, productToOrder => productToOrder.product)
    productToOrders: ProductToOrder[];
}

productToOrder.entity.ts:

import { Order } from "src/order/order.entity";
import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Product } from "./product.entity";

@Entity()
export class ProductToOrder extends BaseEntity{
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    amount: number;

    @ManyToOne(() => Product, product => product.productToOrders,{eager: false})
    product: Product;

    @ManyToOne(() => Order, order => order.productToOrders, {eager: false})
    order: Order;
}

order.entity.ts:

import { User } from "src/auth/user.entity";
import { Product } from "src/product/product.entity";
import { ProductToOrder } from "src/product/productToOrder.entity";
import { BaseEntity, Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Order extends BaseEntity{
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    ordernumber: number;

    @Column()
    totalprice: number;
    
    @ManyToOne(type => User, user => user.orders, {eager: false})
    user_id: User[];

    @OneToMany(() => ProductToOrder, productToOrder => productToOrder.order, {eager: true})
    productToOrders: ProductToOrder[];
}

My directory structure:

src/
├── order
│   ├── order.entity.ts
│   ├── order.module.ts
│   ├── order.service.ts
│   ├── order.controller.ts
│   └── order.repository.ts
├── product
│   ├── product.entity.ts
│   ├── product.module.ts
│   ├── product.service.ts
│   ├── product.controller.ts
│   ├── productToOrder.entity.ts <- I put the entity for the pivot table here, not sure if its right
│   └── product.repository.ts
└── xy

app.module.ts:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { typeOrmConfig } from './config/typeorm.config';
import { ProductModule } from './product/product.module';
import { OrderModule } from './order/order.module';


@Module({
  imports: [
    TypeOrmModule.forRoot(typeOrmConfig),
    AuthModule,
    ProductModule,
    OrderModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

typeorm.config.ts:

import { TypeOrmModuleOptions } from "@nestjs/typeorm";
import * as config from 'config';

const dbConfig = config.get('db');

export const typeOrmConfig: TypeOrmModuleOptions = {
    type: dbConfig.type,
    host: dbConfig.host,
    port: dbConfig.port,
    username: dbConfig.username,
    password: dbConfig.password,
    database: dbConfig.database,
    autoLoadEntities: true,
    synchronize: dbConfig.synchronize,
}

If I add the

entities: ["dist/**/*.entity{.ts,.js}"],

line to my typeorm.config.ts and start the application with npm run start:dev the error changes to:

 TypeError: metatype is not a constructor
like image 893
Exo Avatar asked Apr 13 '26 06:04

Exo


1 Answers

I received the same error and it took me hours to debug. In the end, I had forgotten @Entity() on at least one of my Entity classes.

like image 168
twinlakes Avatar answered Apr 15 '26 20:04

twinlakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!