node-persist
node-persist copied to clipboard
Model for hasOne foreign key issues
I have one table employee have one foreign key with other table department, and the table column and data below: For empoyee
| employee_id | employee_name | department_id |
|---|---|---|
| 1 | Tom Preston-Werner | 1 |
| 2 | Albert Einstein | 1 |
For department
| department_id | department_name |
|---|---|
| 1 | Department1 |
| 2 | Department2 |
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?