Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Save not appending associated foreign key values #148

@m1cr0man

Description

@m1cr0man

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions