RobinCK / typeorm-fixtures

:pill: Fixtures loader for typeorm πŸ‡ΊπŸ‡¦
https://robinck.github.io/typeorm-fixtures/
MIT License
566 stars 45 forks source link

ManyToMany Relations error #67

Closed kop7 closed 5 years ago

kop7 commented 5 years ago

"typeorm-fixtures-cli": "^1.3.5"

Getting error when run fixtures Ads.yml:

Fail fixture loading: Entity metadata for Category#ads was not found.

maybe is bug?

@Entity('ad')
export class Ad {
  @PrimaryGeneratedColumn
  id: number;
  @Column()
  title: string;
  @ManyToMany(type => Category, category => category.ads)
  @JoinTable({ name: 'ad_category' })
  categories: Category[];
}
@Entity('category')
export class Category {
  @PrimaryGeneratedColumn()
  id: number;
  @Column)
  name: string;
  @ManyToMany(type => Ad, ad => ad.categories)
  ads: Ad[];
}

Ads.yml

entity: Ad
items:
  ad{1..100}:
    title: '{{name.jobType}}'
    categories:
      category1:
        name: '{{name.jobArea}}'
leohajder commented 5 years ago

We found the issue. We had this:

// src/entity/index.ts
export * from './ad.entity';
export * from './category.entity';
// ...

and then we imported entities like this, to write less lines and organize better:

import { Ad, Category, WhatEverOtherEntity } from ./;

and not like this:

import { Ad } from ./ad.entity;
import { Category } from ./category.entity;
// separate imports

This seems to be an issue with typeorm-fixtures-cli, even though other stuff works normally and the application builds successfully.

RobinCK commented 5 years ago

please see this issue https://github.com/RobinCK/typeorm-fixtures/issues/58 and this sample https://github.com/RobinCK/typeorm-fixtures-sample I think Ads.yml is not correct you need to create category.yml and ads.yml and set category reference in ads structure

leohajder commented 5 years ago

You are correct, we fixed the fixtures.

entity: Ad
items:
  ad{1..100}:
    title: '{{name.jobType}}'
    categories:
      - '@category*'
      - '@category*'
entity: Category
items:
  category{1..10}:
    name: '{{name.jobArea}}'

However, we had this exact setup earlier. The version @kop7 posted was a result of trying to work around the error. We found that the error was caused soley by the imports. We didn't suspect that because the application has worked well that way. Thank you very much for your help. πŸ₯‡