Unable to assign model to relationship when creating record #4091
Answered
by
simonjcarr
simonjcarr
asked this question in
Help
-
Hi, I have the following User and Doc models //Doc Model
import { 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 assigned_to: HasOne<typeof User>
@hasOne(() => User)
public assigned_by: HasOne<typeof User>
@hasOne(() => User)
public approved_by: HasOne<typeof User>
@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 model
import { 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
} I have this controller method import { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
import Doc from "App/Models/Doc";
import User from "App/Models/User";
export default class DocsController {
public async index({ response }: HttpContextContract) {
const user = await User.findOrFail(1)
console.log(user)
let doc = new Doc();
doc.title = "Test Doc";
doc.link_url = "https://www.google.com";
doc.documentation = "This is a test doc";
doc.assigned_to = user; //*****Errors are here****
doc.assigned_by = user; //****Errors are here*****
doc.approved_by = user; //****Errors are here****
doc.save()
const docs = await Doc.all();
return response.json(docs);
}
} I am getting the following typescript errors on the relationships
I get the following error from the controller when it tries to insert a record using the code above.
The output from
|
Beta Was this translation helpful? Give feedback.
Answered by
simonjcarr
Mar 1, 2023
Replies: 1 comment
-
I have solved my own problem, but assigning the id of the user rather than a User Object. I am sure you used to be able to do this Adonis V4, but it's a while since I have used Adonis, so I could be wrong. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
simonjcarr
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have solved my own problem, but assigning the id of the user rather than a User Object. I am sure you used to be able to do this Adonis V4, but it's a while since I have used Adonis, so I could be wrong.