ManyToMany : Cannot add or update a child row: a foreign key constraint fails #1586
Answered
by
RomainLanz
LeadcodeDev
asked this question in
Help
-
Hi! During my development I encountered a problem concerning the update only of a model having a ManyToMany relation (see attachment). Here is the error and my data doesn't update, do you know why ?
My models : // Achievement model
export default class Achievements extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public title: string
@column()
public description: string
@column()
public url: string
@column()
public pictureId: number
@manyToMany(() => Technology)
public technologies: ManyToMany<typeof Technology>
} // Technologies model
export default class Technology extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public label: string
@column()
public description: string
@column()
public url: string
@column()
public pictureId: number
} My pivot table public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')
table.integer('technology_id').unsigned().references('id').inTable('technologies')
table.integer('achievements_id').unsigned().references('id').inTable('achievements')
})
} My controller : public async update({ request, params }: HttpContextContract) {
const technologies = await request.input('technologies')
const achievement = await Achievement.findOrFail(params.id)
const data = await request.validate({
schema: schema.create({
title: schema.string(),
description: schema.string(),
url: schema.string(),
picture_id: schema.number()
})
})
await achievement.merge(data).related('technologies').sync(technologies)
return { message: 'La ressource a été mis à jour' }
} EDIT: my data (title, description, url) is now saved but my error above is still present |
Beta Was this translation helpful? Give feedback.
Answered by
RomainLanz
Sep 11, 2020
Replies: 1 comment 1 reply
-
Hey @LeadcodeDev! 👋 If you have a closer look to the error, you can see that you are not sending a correct parameter when calling insert into `achievements_technology` (`achievements_id`, `technology_id`) values (1, '[object Object]') You must troubleshoot what you have inside |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
LeadcodeDev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @LeadcodeDev! 👋
If you have a closer look to the error, you can see that you are not sending a correct parameter when calling
sync()
.You must troubleshoot what you have inside
technologies
.