RobinCK / typeorm-fixtures

:pill: Fixtures loader for typeorm 🇺🇦
https://robinck.github.io/typeorm-fixtures/
MIT License
566 stars 45 forks source link

Fail fixture loading: Maximum call stack size exceeded #91

Closed mmarkell closed 4 years ago

mmarkell commented 4 years ago

Probably more of a question than a bug, but I guess we'll see...

I am trying to create fixtures for a table that has several ManyToOne relations.

UserGroup.ts

@Entity()
export default class UserGroup {
  @PrimaryGeneratedColumn()
  public id!: number;

  @ManyToOne(
    () => User,
    user => user.userGroups,
    { primary: true }
  ) // inverse "userGroups: UserGroup[]" is one-to-many in user
  @JoinColumn({ name: 'user_id' })
  user!: User;

  @ManyToOne(
    () => Group,
    group => group.userGroups,
    { primary: true }
  ) // inverse "userGroups: UserGroup[]" is one-to-many in group
  @JoinColumn({ name: 'group_id' })
  group!: Group;

  @Column({ name: 'status' })
  status: UserGroupStatus = UserGroupStatus.ACTIVE;
}

User.ts

@Entity()
export default class User {
...
  @OneToMany(
    _type => UserGroup,
    userGroup => userGroup.user,
    {
      eager: true
    }
  )
  userGroups!: UserGroup[];
}

and then the same for Group.

I am trying to populate the fixtures with:

User.yml

entity: User
items:
  user{1..2000}:
   ...
   userGroups: ["@userGroups*"]
entity: Group
items:
  group{1..2000}:
   ...
   userGroups: ["@userGroups*"]

UserGroup.yml

entity: UserGroup
items:
  userGroups{1..2000}:
    user_id: "@user($current)"
    group_id: "@group($current)"
    status: "{{random.number(4)}}"

But when I run the cli, I get the following error:

Fail fixture loading: Maximum call stack size exceeded
RangeError: Maximum call stack size exceeded
    at Resolver.resolveDeepDependencies (/usr/app/server/node_modules/typeorm-fixtures-cli/dist/Resolver.js:118:31)
  ...

What am I doing wrong? I assume this is because of a circular reference between UserGroups and the User/Group entities, but I don't see how else I can create a relationship between the entities.

RobinCK commented 4 years ago

I think you need to add reference only on one side and then everything will work

Like this

User.yml

entity: User
items:
  user{1..2000}:
   ...
entity: Group
items:
  group{1..2000}:
   ...

UserGroup.yml

entity: UserGroup
items:
  userGroups{1..2000}:
    user: "@user($current)"
    group: "@group($current)"
    status: "{{random.number(4)}}"
mmarkell commented 4 years ago

Wow that worked...thank you so much!