I have a question regarding node orm2 hasMany association, my model definition is like this.
schemas/Channel.js
var model = db.define('channels', Channel, ChannelOptions);
var Channel = {
channel_name : String,
channel_email : String,
channel_id : String,
views : Number
};
var ChannelOptions = {
id : "channel_id",
methods: {
my_details : function (err) {
return this.channel_id +' '+ this.channel_name + ' ' + this.views;
}
}
};
schemas/network.js
var model = db.define('networks', Network, NetworkOptions);
var Channel = require('../schemas/Channel')(db);
model.hasMany('channels', Channel, {}, {autoFetch:true});
model.sync()
db.sync(function(){
console.log('DB SYNCHED');
});
var Network = {
network_id : Number,
name : String,
username : String,
logo : String,
website : String
};
var NetworkOptions = {
id : "network_id",
methods: {
}
};
It created a networks_channels table and I have filled it with a networkID and channelID. it is responding with the property (channels) but it is empty. Is there something missing?
Just figured out what was wrong. Its becauset I have set up the database table definitions before doing db.sync(). Turns out that its doing all the work for me. Clearing up the tables and refilling it with data did the trick.