How do I specify the fields on either side of a hasOne/hasMany relationship #4092
-
The last time I used Adonis was V4, and obviously a lot of things have changed in V5. I am having problems querying what seems like a pretty simple relationship. I think the issue is I need to specify the name of the fields on either side of the relationship. I have Two models User and Doc Here is the code for them Doc Modelimport { DateTime } from 'luxon'
import { BaseModel, column, hasOne, HasOne } from '@ioc:Adonis/Lucid/Orm'
import User from './User'
export default class Doc extends BaseModel {
@hasOne(() => User)
public assignedToUser: HasOne<typeof User>
@hasOne(() => User)
public assignedByUser: HasOne<typeof User>
@hasOne(() => User)
public approvedByUser: HasOne<typeof User>
@column()
public assigned_to: number
@column()
public assigned_by: number
@column()
public approved_by: number
@column({ isPrimary: true })
public id: number
@column()
public title: string
@column()
public link_url: string
@column()
public documentation: string
@column()
public approved_date: DateTime
@column()
public approval_comment: string
@column()
public use_for_build_doc: number
@column()
public build_doc_title: string
@column()
public build_doc_link_url: string
@column()
public build_doc_documentation: string
@column.dateTime({ autoCreate: true })
public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
} User Modelimport { DateTime } from 'luxon'
import { BaseModel, column, HasMany, hasMany } from '@ioc:Adonis/Lucid/Orm'
import Doc from './Doc'
export default class User extends BaseModel {
@hasMany(() => Doc)
public assignedToDocs: HasMany<typeof Doc>
@hasMany(() => Doc)
public assignedByDocs: HasMany<typeof Doc>
@hasMany(() => Doc)
public approvedByDocs: HasMany<typeof Doc>
@column({ isPrimary: true })
public id: number
@column()
public first_name: string
@column()
public last_name: string
@column()
public email: string
@column.dateTime({ autoCreate: true })
public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
} Controller with the query I am having problems withexport default class DocsController {
public async index({ response }: HttpContextContract) {
const user = await User.findOrFail(1)
const docs = await Doc.query().preload('assignedToUser').preload('assignedByUser').preload('approvedByUser')
docs.forEach((doc) => {
console.log(doc.assignedToUser)
console.log(doc.assignedByUser)
console.log(doc.approvedByUser)
})
return response.json(docs);
}
} The above query gives me the following error
The error I want the following relationships to exist
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I solve the issue with the following additions to my relationships in each model Doc Model@hasOne(() => User, {
localKey: 'assigned_to',
foreignKey: 'id'
})
public assignedToUser: HasOne<typeof User>
@hasOne(() => User, {
localKey: 'assigned_by',
foreignKey: 'id'
})
public assignedByUser: HasOne<typeof User>
@hasOne(() => User, {
localKey: 'approved_by',
foreignKey: 'id'
})
public approvedByUser: HasOne<typeof User> User Model@hasMany(() => Doc, {
foreignKey: 'assigned_to'
})
public assignedToDocs: HasMany<typeof Doc>
@hasMany(() => Doc, {
foreignKey: 'assigned_by'
})
public assignedByDocs: HasMany<typeof Doc>
@hasMany(() => Doc, {
foreignKey: 'approved_by'
})
``` |
Beta Was this translation helpful? Give feedback.
I solve the issue with the following additions to my relationships in each model
Doc Model
User Model