一切都在这里工作。唯一的问题是,在我的手柄文件中,我必须得到一个人的任务:
this.Tasks
我宁愿让它看起来像这样:
this.tasks
我如何自定义Sequelize?
这就是我的应用程序的根路径(它呈现index.handlebars文件)
我的路线:
router.get('/', function(req, res) {
models.Person.findAll({
include: [ models.Task ]
}).then(function(people) {
res.render('index', {
user_id: req.session.user_id,
email: req.session.user_email,
logged_in: req.session.logged_in,
people: people
});
});
});
我的index.handlebars文件:
{{#each people}}
<li>
{{this.name}}
{{#if ../logged_in}}
<a href="/people/{{this.id}}/destroy"> destroy</a>
{{/if}}
<ul>
{{#if ../logged_in}}
<li>
<form action="/people/{{this.id}}/tasks/create" method="POST" style="display: inline">
<input type="text" name="task" placeholder="add task here">
<input type="submit" value"assign task">
</form>
</li>
{{/if}}
{{#each this.Tasks }}
<li>
{{this.task}}
{{#if ../../logged_in}}
<a href="/people/{{this.person_id}}/tasks/{{this.id}}/destroy">destroy</a>
{{/if}}
</li>
{{/each}}
</ul>
</li>
{{/each}}
我的人员表迁移:
"use strict";
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface
.createTable('people', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: Sequelize.STRING,
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE
});
},
down: function(queryInterface, Sequelize) {
return queryInterface
.dropTable('people');
}
};
我的任务表迁移:
"use strict";
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface
.createTable('tasks', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
person_id: {
type: Sequelize.INTEGER
},
task: Sequelize.STRING,
created_at: Sequelize.DATE,
updated_at: Sequelize.DATE
});
},
down: function(queryInterface, Sequelize) {
return queryInterface
.dropTable('tasks');
}
};
my person.js model:
"use strict";
module.exports = function(sequelize, DataTypes) {
var Person = sequelize.define("Person", {
name: DataTypes.STRING
}, {
underscored: true,
freezeTableName: true,
tableName: 'people',
classMethods: {
associate: function(models) {
Person.hasMany(models.Task)
}
}
});
return Person;
};
我的task.js模型:
"use strict";
module.exports = function(sequelize, DataTypes) {
var Task = sequelize.define("Task", {
task: DataTypes.STRING
}, {
freezeTableName: true,
tableName: 'tasks',
classMethods: {
associate: function(models) {
Task.belongsTo(models.Person, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
}
});
return Task;
};
答案 0 :(得分:2)
您也可以使用'as'选项通过别名设置名称,而不是使用sequelize.define的小写名称。防爆。 Task.belongsTo(models.Person, {as: 'task'});
。或者,您可以在define函数本身中设置别名。防爆。
sequelize.define("Task", attributes,
{
name : {
singular: "task",
plural: "tasks"
})
答案 1 :(得分:1)
将模型名称以小写形式定义为
var Task = sequelize.define(“task”,{
答案 2 :(得分:0)
我的task.js模型:
module.exports = function(sequelize, DataTypes) { var Task = sequelize.define("Task", {
只需指定
即可var task = sequelize.define("task", {
它没有解决你的问题吗?