AdonisJS通过hasOne实现hasManyThrough。

huangapple go评论97阅读模式
英文:

AdonisJS hasManyThrough through a hasOne

问题

我有以下模型:

Usuario 有许多 RolesUsuario 属于 Rol

我想在 Usuario 中设置一个 hasManyThrough,以便我可以使用更简单的预加载访问用户的角色列表。 hasManyThrough 的文档没有完全解释如何建模这种情况。

export default class Rol extends BaseModel {
  public static table = 'roles';

  @hasMany(() => RolUsuario, {
    foreignKey: 'idRol',
  })
  public rolesUsuarios: HasMany<typeof RolUsuario>;

  @column({ isPrimary: true })
  public id: number;

  @column()
  public nombre: string;
}
export default class RolUsuario extends BaseModel {
  public static table = 'roles_usuarios';

  @belongsTo(() => Usuario, {
    foreignKey: 'idUsuario',
  })
  public usuario: BelongsTo<typeof Usuario>;

  @belongsTo(() => Rol, {
    foreignKey: 'idRol',
  })
  public rol: BelongsTo<typeof Rol>;

  @column({ isPrimary: true })
  public id: number;

  @column()
  public idUsuario: string;

  @column()
  public idRol: string;
}
export default class Usuario extends BaseModel {
  @hasMany(() => RolUsuario, {
    foreignKey: 'idUsuario',
  })
  public rolesUsuarios: HasMany<typeof RolUsuario>;

  @hasManyThrough([() => Rol, () => RolUsuario], { // 帮助!!!
    foreignKey: 'idUsuario',
    throughForeignKey: 'idRol',
  })
  public roles: HasManyThrough<typeof Rol>;

  @column({ isPrimary: true })
  public id: string;
}

然后当我这样做时:

await Usuario.query().preload('roles')

我得到这个错误:

"E_MISSING_MODEL_ATTRIBUTE: "Usuario.roles" expects "idRol" to exist on "Rol" model, but is missing"
英文:

I have the following models:

Usuario has many RolesUsuario belongsTo Rol

I want to setup a hasManyThrough in Usuario so that I can access the list of roles for a user with a simpler preload. The documentation for the hasManyThrough doesn't fully explain how to model this scenario.

export default class Rol extends BaseModel {
  public static table = &#39;roles&#39;

  @hasMany(() =&gt; RolUsuario, {
    foreignKey: &#39;idRol&#39;,
  })
  public rolesUsuarios: HasMany&lt;typeof RolUsuario&gt;

  @column({ isPrimary: true })
  public id: number

  @column()
  public nombre: string
}
export default class RolUsuario extends BaseModel {
  public static table = &#39;roles_usuarios&#39;

  @belongsTo(() =&gt; Usuario, {
    foreignKey: &#39;idUsuario&#39;,
  })
  public usuario: BelongsTo&lt;typeof Usuario&gt;

  @belongsTo(() =&gt; Rol, {
    foreignKey: &#39;idRol&#39;,
  })
  public rol: BelongsTo&lt;typeof Rol&gt;

  @column({ isPrimary: true })
  public id: number

  @column()
  public idUsuario: string

  @column()
  public idRol: string
}
export default class Usuario extends BaseModel {
  @hasMany(() =&gt; RolUsuario, {
    foreignKey: &#39;idUsuario&#39;,
  })
  public rolesUsuarios: HasMany&lt;typeof RolUsuario&gt;

  @hasManyThrough([() =&gt; Rol, () =&gt; RolUsuario], { // HELP!!!
    foreignKey: &#39;idUsuario&#39;,
    throughForeignKey: &#39;idRol&#39;,
  })
  public roles: HasManyThrough&lt;typeof Rol&gt;

  @column({ isPrimary: true })
  public id: string
}

Then when I do this:

await Usuario.query().preload(&#39;roles&#39;)

I get this error:

&quot;E_MISSING_MODEL_ATTRIBUTE: &quot;Usuario.roles&quot; expects &quot;idRol&quot; to exist on &quot;Rol&quot; model, but is missing&quot;

答案1

得分: 1

我不完全明白为什么但这个代码有效
```javascript 
  @hasManyThrough([() => Rol, () => RolUsuario], {
    foreignKey: 'idUsuario',
    throughForeignKey: 'id',
    throughLocalKey: 'idRol',
  })
  public roles: HasManyThrough<typeof Rol>
英文:

Okay I don't fully understand why but this works:

  @hasManyThrough([() =&gt; Rol, () =&gt; RolUsuario], {
    foreignKey: &#39;idUsuario&#39;,
    throughForeignKey: &#39;id&#39;,
    throughLocalKey: &#39;idRol&#39;,
  })
  public roles: HasManyThrough&lt;typeof Rol&gt;

huangapple
  • 本文由 发表于 2023年1月9日 04:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定