Skip to content

Relational mapping HasMany and HasOne not working properly #109

@etuscano

Description

@etuscano

Hi,
In our company we are trying to migrate few applications from java to node js .
As part of the ORM we choose to use node -persist seeing lot of valuable features and it will go long way...

after doing the relational one to many and many to many mapping we found result is not retrieving properly from database.

we created tables in database

CREATE TABLE Person
(
ID int NOT NULL,
NAME varchar2(1000) ,
PHONE_ID int NOT NULL,
PRIMARY KEY (PHONE_ID)
);

CREATE TABLE Phone
(
ID int NOT NULL PRIMARY KEY,
PHONE_ID int NOT NULL,
PHONE_NUMBER int NOT NULL,
CONSTRAINT PHONE_ID FOREIGN KEY (PHONE_ID)
REFERENCES Person(PHONE_ID)
);

node js code

var persist = require('persist');
var type = persist.type;
Phone = persist.define("Phone", {
"id": {
type: type.INTEGER, dbColumnName: 'ID',primaryKey:'ID'
},"PHONE_ID": {
type: type.INTEGER, dbColumnName: 'PHONE_ID'
},"phone_number": {
type: type.INTEGER, dbColumnName: 'PHONE_NUMBER'
}
}, {
"tableName": "PHONE"
});
Person = persist.define("Person", {
"id": {
type: type.INTEGER, dbColumnName: 'ID'
},
"name": {
"type": type.STRING, dbColumnName: 'NAME'
},"PHONE_ID": {
type: type.INTEGER, dbColumnName: 'PHONE_ID',primaryKey:'PHONE_ID'
}
}, {
"tableName": "PERSON"
}).hasMany(Phone,{foreignKey:'PHONE_ID'});

persist.connect({
"driver": "oracle",
"name": "xxxxxx",
"hostname": "xxxxxxxxx",
"user": "ecee_int",
"password": "xxxxxx",
"port": "1521",
"database": "xxxxx",
"pooling": {
"name": "xxxxxxx",
"max": 2,
"min": 1,
"idleTimeoutMillis": 30000
}
}, function(err, connection) {
Person.using(connection).all(function(err, person) {
person.phones.orderBy('number').all(function(err, phones) {
console.log(JSON.stringify(phones));
});
});

});

but the result returned is [{"phones":{},"id":1,"name":"karthik"},{"phones":{},"id":2,"name":"asuthos"},{"phones":{},"id":3,"name":"peter"}]

which don't have associated phones
kindly suggest us what is wrong here ?

thanks
Elcina

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