Creating n:m Table
//my Task.model.js
export default function(sequelize, DataTypes) {
return sequelize.define('Task', {
_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name:{
type :DataTypes.STRING(50),
allowNull: false
},
notice:{
type :DataTypes.STRING(255),
allowNull: true
},
user_id: {
type: DataTypes.INTEGER,
allowNull: true
},
createdBy: {
type: DataTypes.INTEGER,
allowNull: true
},
lang_id: {
type: DataTypes.INTEGER,
allowNull: true
},
status_id: {
type: DataTypes.INTEGER,
allowNull: false
}
});
}
//my key model.js
export default function(sequelize, DataTypes) {
return sequelize.define('dict_Keys', {
_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name:{
type :DataTypes.STRING(50),
allowNull: false,
unique: {
msg: 'The specified Key is already in use.'
}
},
notice: {
type: DataTypes.STRING(255),
allowNull: true
},
user_id: {
type: DataTypes.INTEGER,
allowNull:true
},
});
}
// my Index.js where everything is set
var TaskKeys = db.sequelize.define('TaskKeys',{
value: Sequelize.STRING(255)
})
// Tasks n:m Keys
db.DictKey.belongsToMany(db.Task, { through: TaskKeys, foreignKey:'key_id',otherKey:'task_id'});
db.Task.belongsToMany(db.DictKey, { through: TaskKeys, foreignKey: 'task_id',otherKey: 'key_id'});
module.exports = db;
// my task.controller.js with all functions
// Creates a new Task in the DB
export function create(req, res) {
return Task.create({
name: req.body.task.name,
user_id: req.body.task.user_id,
lang_id: req.body.task.lang_id,
createdBy: req.body.task.createdBy,
status_id : 1,
notice: req.body.task.notice
}).then((task) => {
return task.addDict_Keys(req.body.keys); // this is working fine
})
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
Like this im getting all Keys with the asked TaskId
return Task.findAll({
where:{
_id: req.params.id
},
include: [{
model: DictKey,
through: {
where: {
key_id: [1,2,3,4,7]
}
}
}]
}
)
.then(respondWithResult(res))
And I get the TaskKey table with values->null. Everything fine.
Doing this with Sequelize Code now
return Task.getDict_Keys({where:{
task_id : 66
}})
.then(respondWithResult(res))
Is throwing its not a Function.
Not a Function
TypeError: _sqldb.Task.getDict_Keys is not a function
Did i somethign wrong in creating n:m Table?
How can i retrieve Data in Column value from TaskKey and Update it ?
Dialect: mssql
__Database version: slq server 2014
Sequelize version: 3.5.1
In future I want to Update the value in TaskKey value und get the Data from it.
So that is the reason i try get and later will use set? if its for updating right?
Inspect Task.Instance.prototype
to see method naming.
@mickhansen
// Updates an existing Task in the DB
console.log(Task.Instance.prototype);
return Task.getDict_Keys({
where: {
_id: 66
}
})
.then(handleEntityNotFound(res))
//.then(saveUpdates2(req.body))
.then(respondWithResult(res))
//.catch(handleError(res));
}
The console.log output is
_isAttribute: { [Function] cache: MapCache { __data__: [Object] } },
Model: Task,
'$Model': Task,
getDict_Lang: [Function],
setDict_Lang: [Function],
createDict_Lang: [Function],
getStatus: [Function],
setStatus: [Function],
createStatus: [Function],
getUser: [Function],
setUser: [Function],
createUser: [Function],
getCreatedByFk: [Function],
setCreatedByFk: [Function],
createCreatedByFk: [Function],
getDict_Keys: [Function],
countDict_Keys: [Function],
hasDict_Keys: [Function],
hasDict_Key: [Function],
setDict_Keys: [Function],
addDict_Key: [Function],
addDict_Keys: [Function],
removeDict_Key: [Function],
removeDict_Keys: [Function],
createDict_Key: [Function] }
So getDict_Keys is defined.
As well addDict_Keys what is allready working.
But other ones like get/count are not working.
Are you sure you are calling them on an instance and not the model?
//index.js
// the instances, which i import in my controller.js to work with
db.Task = db.sequelize.import('../api/task/task.model');
db.DictKey = db.sequelize.import('../api/dict_key/dict_key.model');
var TaskKeys = db.sequelize.define('TaskKeys',{
value: Sequelize.STRING(255)
})
// Tasks n:m Keys
db.DictKey.belongsToMany(db.Task, { through: TaskKeys, foreignKey:'key_id',otherKey:'task_id'});
db.Task.belongsToMany(db.DictKey, { through: TaskKeys, foreignKey: 'task_id',otherKey: 'key_id'});
//controller.js
// Creates a new Task in the DB
export function create(req, res) {
return Task.create({
name: req.body.task.name,
...
}).then((task) => {
return task.addDict_Keys(req.body.keys);//in create add is working
})
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
//in update
// Updates an existing Task in the DB
export function update(req, res) {
//console.log(Task.Instance.prototype);
return Task.findAll({
where:{
_id: req.params.id
},
include: [{
model: DictKey,
through: {
where: {
key_id: [1,2,3,4,7]
}
}
}]
}
)
.then((res)=>{//trying with result as in add
console.log(res.getDict_Keys());// not a function
})
.then(handleEntityNotFound(res))
//.then(saveUpdates2(req.body))
.then(respondWithResult(res))
//.catch(handleError(res));
}
//trying only get
// Updates an existing Task in the DB
export function update(req, res) {
console.log(Task.Instance.prototype);
return Task.getDict_Keys() //not a function
.then(handleEntityNotFound(res))
//.then(saveUpdates2(req.body))
.then(respondWithResult(res))
//.catch(handleError(res));
}
Even in index.js where i set everything up it is not working
var TaskKeys = db.sequelize.define('TaskKeys',{
value: Sequelize.STRING(255)
})
// Tasks n:m Keys
db.DictKey.belongsToMany(db.Task, { through: TaskKeys, foreignKey:'key_id',otherKey:'task_id'});
db.Task.belongsToMany(db.DictKey, { through: TaskKeys, foreignKey: 'task_id',otherKey: 'key_id'});
console.log(db.Task.Instance.prototype);//output list like comment before where getDict_keys is stated as possible function
db.Task.getDict_Keys(); //not a function
Most helpful comment
Inspect
Task.Instance.prototype
to see method naming.