/*
* @Description:
* @Author: 熊成强
* @Date: 2019-07-29 11:47:34
* @LastEditTime: 2019-08-02 11:03:06
* @LastEditors: 熊成强
*/
var express = require('express');
var forToken = require('../token');
var router = express.Router();
const users = require('../server/User.js')
const mode = require('../server/index.js')
const sequelize = require('sequelize');
const cache = require('../redis')
var log = require('log4js').getLogger("index");
/* GET users listing. */
// 多对多
router.get('/', function (req, res, next) {
mode.User.findAll({
raw: false,
attributes: ['user_name'],
where: {
// teacher_name: "张老师"
},
include: [{
model: mode.Teacher,
attributes: [
'teacher_name'
],
through: { //除去中间表
attributes: []
},
where: {
teacher_name: {
[sequelize.Op.like]: '张%'
}
}
}]
}).then(r => {
console.log(r)
res.send({
code: '200',
data: {
code: '200',
data: r
}
})
})
// return
//一对多
// mode.School.findAll({
// raw: false,
// attributes: ['school_name'],
// include: [{
// model: mode.User,
// attributes: ['user_name'],
// where: {
// user_name: {
// [sequelize.Op.like]: '%1%'
// }
// }
// }]
// }).then(r => {
// console.log(r)
// res.send({
// code: '200',
// data: {
// code: '200',
// data: r
// }
// })
// })
});
module.exports = router;
/*
* @Description:
* @Author: 熊成强
* @Date: 2019-07-29 11:47:34
* @LastEditTime: 2019-08-02 11:05:10
* @LastEditors: 熊成强
*/
var fs = require('fs');
const mode = {}
fs.readdirSync(__dirname).filter((value) => {
return value !== 'index.js'; //过滤自身
}).map(value => mode[value.split('.')[0]] = require('./' + value));
console.log(mode)
//为用户和学校建立关系 一对多
mode.School.hasMany(mode.User, {
foreignKey: 'school_id',
sourceKey: 'id',
constraints: false
})
mode.User.belongsTo(mode.School, {
foreignKey: 'school_id',
targetKey: 'id',
constraints: false
})
//多对多
mode.Teacher.belongsToMany(mode.User, {
through: mode.TeachToUser,
foreignKey: 'teacher_id',
constraints: false
})
mode.User.belongsToMany(mode.Teacher, {
through: mode.TeachToUser,
foreignKey: 'user_id',
constraints: false
})
module.exports = mode;