node-persist icon indicating copy to clipboard operation
node-persist copied to clipboard

Model for hasOne foreign key issues

Open zorrofox opened this issue 12 years ago • 0 comments

I have one table employee have one foreign key with other table department, and the table column and data below: For empoyee

employee_idemployee_namedepartment_id
1Tom Preston-Werner1
2Albert Einstein1

For department

department_iddepartment_name
1Department1
2Department2

I have this model define and query below:

...
department = persist.define('department', {
    'id' : {
        type : type.INTEGER,
        dbColumnName : 'department_id',
        primaryKey : true
    },
    'department_name' : type.STRING
});

employee = persist.define('employee', {
    'id' : {
        type : type.INTEGER,
        dbColumnName : 'employee_id',
        primaryKey : true
    },
    'employee_name' : type.STRING
}).hasOne(department);

persist.connect({
    "driver" : "oracle",
    "hostname" : "localhost",
    "user" : "hr",
    "password" : "welcome1",
    trace : true
}, function(err, connection) {
    employee.include('department').all(connection, function(err, employees) {
        if(err)
              console.log(err);     
    });

});
...

But the ORM will return this SQL to me:

select t0.employee_id AS c0,
 t0.employee_name AS c1,
 t0.department_id AS c2,
 t1.department_id AS c3,
 t1.department_name AS c4 
FROM employees t0 
LEFT JOIN departments t1 ON t1.employee_id=t0.department_id  

It is very stange the ORM will use the employee_id as the join key. Anyone also has this issue?

zorrofox avatar May 25 '13 16:05 zorrofox