简体   繁体   English

TypeORM - EntitySchema 中定义的一对多关系

[英]TypeORM - One-To-Many relationship defined in EntitySchema

I'm using TypeORM with JS and it's docs are super minimal, so I couldn't find the solution for my question over there.我将 TypeORM 与 JS 一起使用,它的文档非常少,所以我在那里找不到我的问题的解决方案。 I want to create a one-to-many bi-directional relationship and can't get over the following exception:我想创建一个一对多的双向关系并且无法克服以下异常:

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

Thats the definition of the two entities (one team might have multiple groups):这就是两个实体的定义(一个团队可能有多个组):

module.exports = new EntitySchema({
  name: 'Team',
  tableName: 'team',
  columns: {
    id: {
      primary: true,
      type: 'uuid',
      generated: true,
    },
    name: {
      type: 'varchar',
    },
    
  },
  relations: {
    groups: {
      target: 'TeamGroups',
      type: 'one-to-many',
      inverseSide: 'team_id',
    },
  },
})
module.exports = new EntitySchema({
  name: 'TeamsGroups',
  tableName: 'team_group',
  columns: {
    team_id: {
      primary: true,
      type: 'uuid',
    },
    group_id: {
      primary: true,
      type: 'uuid',
    },
  },
  relations: {
    team_id: {
      target: 'Team',
      type: 'many-to-one',
      joinColumn: true,
    },
  },
})

What am I missing?我错过了什么?

I can't test but I'm fairly sure it's this, looking at the error & docs:我无法测试,但我很确定就是这样,查看错误和文档:

module.exports = new EntitySchema({
  name: 'Team',
  tableName: 'team',
  columns: {
    id: {
      primary: true,
      type: 'uuid',
      generated: true,
    },
    name: {
      type: 'varchar',
    },
    
  },
  relations: {
    TeamGroups: {
      target: 'TeamGroups',
      type: 'one-to-many',
      inverseSide: 'team_id',
    },
  },
})
module.exports = new EntitySchema({
  name: 'TeamsGroups',
  tableName: 'team_group',
  columns: {
    team_id: {
      primary: true,
      type: 'uuid',
    },
    group_id: {
      primary: true,
      type: 'uuid',
    },
  },
  relations: {
    Team: {
      target: 'Team',
      type: 'many-to-one',
      joinColumn: true,
    },
  },
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM