sequelize多表查询问题
sequelize多表查询时报错:include.model.getTableName is not a function
user表结构
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
var hs_user = sequelize.define('hs_user', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
user_name: {
type: DataTypes.CHAR(20),
allowNull: false,
defaultValue: ''
},
password: {
type: DataTypes.CHAR(128),
allowNull: false,
defaultValue: ''
},
sex: {
type: DataTypes.INTEGER(11),
allowNull: false
},
time: {
type: DataTypes.DATE,
allowNull: false
},
address: {
type: DataTypes.STRING(256),
allowNull: false,
defaultValue: ''
},
register_type: {
type: DataTypes.INTEGER(11),
allowNull: false
},
id_type: {
type: DataTypes.INTEGER(11),
allowNull: false
},
id_number: {
type: DataTypes.BIGINT,
allowNull: false
},
community_name: {
type: DataTypes.STRING(256),
allowNull: false,
defaultValue: ''
}
}, {
freezeTableName: true
});
hs_user.associate = function(models){
this.hasMany( models.hs_unlock_log, {foreignKey: 'id'})
}
return hs_user;
};
unlock表结构
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
var hs_unlock_log = sequelize.define('hs_unlock_log', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
unlock_type: {
type: DataTypes.INTEGER(10),
allowNull: false
},
unlock_time: {
type: DataTypes.DATE,
allowNull: false
},
remark: {
type: DataTypes.CHAR(128),
allowNull: true
},
user_id: {
type: DataTypes.INTEGER(11),
allowNull: false,
references: {
model: 'hs_user',
key: 'id'
}
}
}, {
freezeTableName: true
});
hs_unlock_log.associate = function(models){
this.belongsTo( models.hs_user,{foreignKey: 'user_id'} );
}
return hs_unlock_log;
};
model层进行查询
import db from '../config/db.js'
const userModel = '../schema/hs_user.js'
const unlockModel = '../schema/hs_unlock_log.js'
const hsDx = db.hsDx
const user = hsDx.import(userModel)
const unLock = hsDx.import(unlockModel)
// 获取开锁日志
const getUnlockLog = async function(){
let logList = await unLock.findAll({
'include': [{model: unlockModel,}]
});
console.log(logList)
return logList;
}
export default {
getUnlockLog,
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论