node-persist
node-persist copied to clipboard
Issue with hasMany. Associated entities do not persist.
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.