Skip to content

Issue with hasMany. Associated entities do not persist. #102

@olivierntk

Description

@olivierntk

Trying to persist an object with its child associated entities via a hasMany relationship.

Here is the snippet:

'use strict';

var persist = require('persist');
var type = persist.type;

// define some model objects

var Employee = persist.define('Employee', {
  'id': {
    'type': type.INTEGER,
    'dbColumnName': 'ID',
    'primaryKey': true
  },
  'name': {
    'dbColumnName': 'NAME',
    'type': type.STRING
  },
  'ssn': {
    'dbColumnName': 'SSN',
    'type': type.STRING
  },
  'companyId': {
    'dbColumnName': 'COMPANY_ID',
    'type': type.INTEGER
  }
});

var Company = persist.define('Company', {
  'id': {
    'type': type.INTEGER,
    'dbColumnName': 'ID',
    'primaryKey': true
  },
  'name': {
    'dbColumnName': 'NAME',
    'type': type.STRING
  }
}).hasMany(Employee);


var employee1 = new Employee({'name':'Joe Smith', 'ssn': '123-11-1234'});
var employee2 = new Employee({'name':'Marc Grant', 'ssn': '123-11-0000'});
var company1 = new Company({'name': 'intuit', employees: [employee1, employee2]});

persist.connect(function(err, connection) {
  company1.save(connection, function(err) {
    console.log(err);
  });
});

In the examples, the foreign key isn't usually declared. But since in the db, the foreign key name is called 'COMPANY_ID', I added a companyId property in the Employee model.

Not sure what I am doing wrong here. Any help would be appreciated.

Also, the trace tells me an INSERT stmt was run against the companies table but not against the Employees table.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions