I'm trying to use the associations system to insert data into 3 tables...
projects -----< jobs -----< stages
fk_cols: project job
Here's the code I'm running (I'm generalizing the columns where possible):
const db = patio.createConnection('mysql://site:pwdhash@localhost:3306/my_db');
const Project = patio.addModel('projects').oneToMany('jobs');
const Job = patio.addModel('jobs').manyToOne('projects').oneToMany('stages');
const Stage = patio.addModel('stages').manyToOne('jobs');
Project.save({
name: 'test',
jobs: [{
blockNumber: 'P1300',
quantity: 1,
dimensions: '3x2x1',
material: '1730',
grind: 'FG',
stages: [{
name: 'design',
complete: false
}]
}]
}).chain(console.log, console.log);
The following error is produced:
QueryError : ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails ("my_db"."jobs", CONSTRAINT "jobs_ibfk_1" FOREIGN KEY ("project") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE): INSERT INTO "jobs" ("blockNumber", "quantity", "dimensions", "material", "grind") VALUES ('P1300', 1, '3x2x1', '1730', 'FG')
Any ideas on a cause & solution?