Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
olivierntk opened this issue Aug 25, 2014 · 0 comments
Open

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

olivierntk opened this issue Aug 25, 2014 · 0 comments

Comments

@olivierntk
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant