TypeORM多态关系无法从父级获取数据

问题描述 投票:0回答:2

所以我在模型中使用 https://github.com/bashleigh/typeorm-polymorphic 作为多态关系。有几个型号

// Device Entity
@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export class Device extends BaseModel { // Base model is just primary generated id and timestamp
// Device stuff
}
// lock entity
@ChildEntity()
export class Lock extends Device {
// Lock stuff

@PolymorphicChildren(()=>ConnectionData, {
        eager: false
      })
providers: ConnectionData[]
}
// connection data entity
@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export class ConnectionData extends BaseModel  {
// connection data basic stuff
}
// first user type entity
@ChildEntity()
export class FirstUserType extends ConnectionData implements PolymorphicChildInterface {
    // other first user type stuff

    @PolymorphicParent(()=>[Lock,Key]) // Key is also a parent like lock
    connectable: Lock | Key

    @Column({name: 'connectableId'})
    entityId: string;

    @Column({name: 'connectableType'})
    entityType: string;
}

使用这些脚本

let repo = connection.getCustomRepository(FirstUserTypeRepository) // extends AbstractPolymorphicRepository

let result = repo.findOne(1) // find with some id

我能够获取这些数据

{
   id: // first user type id
   prop: // first user type other properties
   connectable : {
     // Lock object
   }
}

但我想要另一种方式。我想要的输出是

{
  id: //some lock id
  data: // some lock data
  providers: [
    // I want here to be list of ConnectionData
  ]
}

我尝试创建这些我认为能够做这样的事情的脚本

let repo = connection.getCustomRepository(LockRepository) // extends AbstractPolymorphicRepository

let result = repo.findOne(1) // find with lock id

但出现这些错误

TypeORMError: Function parameter isn't supported in the parameters. Please check "orm_param_1" parameter.

我不确定如何获取数据。我已经花了几天时间这样做,但直到现在我仍然没有运气。

javascript typescript polymorphism typeorm
2个回答
2
投票

我发现我可以使用TypeOrm InnerJoinAndMap来实现我想要的,我做到了

 const locks = connection.getRepository(Lock)
        const result = await locks
            .createQueryBuilder("lock")
            .innerJoinAndMapMany("lock.providers",ConnectionData,"pc", `pc."connectableId"::text = lock.id::text`)
            .where({
                id:'a725b986-71d7-4f65-bbbf-26f537c13026'
            })
            .getOne()

得到了我想要的结果


0
投票

发生这种情况是由于包中的错误。我创建了一个 pull request 并进行了修复。

© www.soinside.com 2019 - 2024. All rights reserved.