Search code examples
nestjstypeorm

One to many Relation typeorm


I want to create a one-to-many relationship between two tables in typerom (sqlite) But This creates an error

Error Entity metadata for Todo#menus was not found.

My Entity Todo

    @Entity('Todo')
export class Todo{
@PrimaryGeneratedColumn()
id:number

@Column()


text:string

@Column({default:false})

completed:boolean

@Column({nullable:true})

completeTime : Date


@CreateDateColumn()

createTime:Date

@UpdateDateColumn()

updateTime:Date

 @OneToMany(type => Menu,menu => menu.todo)

  menus:Menu[]
  }

Menu Entity =>

 @Entity('Menu')
export  class Menu{

    @PrimaryGeneratedColumn()

    id : number

    @Column()

    Name_Category_Restourent : String 

    @Column()

    Name_Category_Sandewith : String ;

    @Column()

    Name_Category_Drink : String
     @ManyToOne(type=>Todo , todo => todo.menus)
     todo : Todo ;
}

And Config

imports:[
    TypeOrmModule.forRoot({
        type:'sqlite',
        database : 'app.db',
        entities:[__dirname + '/**/*.entity.{js,ts}'],
        synchronize:true
    
    }),
    TypeOrmModule.forFeature([
        Todo,
        Menu
    ]),

Solution

  • The error is coming when the entities are not imported or configured incorrectly.

    Please update the config like below:

    imports:[
        TypeOrmModule.forRoot({
            type:'sqlite',
            database : 'app.db',
            // it seems the path is not correct
            // so you can just import your entities directly here
            entities:[Todo, Menu],
            synchronize:true
        
        })