英文:
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 = '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], { // HELP!!!
foreignKey: 'idUsuario',
throughForeignKey: 'idRol',
})
public roles: HasManyThrough<typeof Rol>
@column({ isPrimary: true })
public id: string
}
Then when I do this:
await Usuario.query().preload('roles')
I get this error:
"E_MISSING_MODEL_ATTRIBUTE: "Usuario.roles" expects "idRol" to exist on "Rol" model, but is missing"
答案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([() => Rol, () => RolUsuario], {
foreignKey: 'idUsuario',
throughForeignKey: 'id',
throughLocalKey: 'idRol',
})
public roles: HasManyThrough<typeof Rol>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论