我的模型定义是
// models / chat_message.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var ChatMessage = sequelize.define('chat_message', {
message: DataTypes.TEXT,
message_type: DataTypes.STRING
}, {
underscored: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt : 'deleted_at',
paranoid: true
}, {
classMethods: {
associate: function(models) {
}
}
});
return ChatMessage;
};
// models / chat_message_stat.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var ChatMessageStat = sequelize.define('chat_message_stat', {
chat_message_id: DataTypes.INTEGER,
delivered: DataTypes.BOOLEAN
}, {
underscored: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt : 'deleted_at',
paranoid: true
}, {
classMethods: {
associate: function(models) {
}
}
});
return ChatMessageStat;
};
//导入这样的模型
var ChatMessage = sequelize.import("./models/chat_message");
var ChatMessageStat = sequelize.import("./models/chat_message_stat");
//然后使用stat
检索所有邮件的行ChatMessage.hasMany(ChatMessageStat);
ChatMessageStat.belongsTo(ChatMessageStat);
ChatMessage.findAndCountAll({
where: params,
include : [{model:ChatMessageStat}]
}).then(function(all_messages) {
// do something with messages
});
以上仅包含作品
时sequelize.sync({force:true}).then(function () {
//
});
但与{force:true}同步会丢弃,并创建我不想要的所有表格,如果我必须重新启动应用程序。
Sequelize文档没有向我显示正确的方向 - 这里有任何输入吗?
我使用sequelize-cli创建迁移 - 仅在迁移中定义外键约束。所以我实际上并不需要sync()来创建foreigns键。