关于本文主要的内容为多对多表之间的关联查询,如何进行绑定表之间的关系的,一对一,一对多,在前面的文章当中上和中文章当中都已经介绍过了,本文就详细的来带大家来看看多对多的玩法首先需要创建三张表,以及对应的测试数据,创建模型代码如下:
// 1.导入Sequelize
const Sequelize = require('sequelize');
(async () => {
// 2.配置连接信息
const sequelize = new Sequelize('demo', 'root', 'yangbuyiya', {
// MySQL服务器地址
host: 'www.yangbuyi.top',
// MySQL服务器端口号
port: 3310,
// 注意点: Sequelize不仅仅能操作MySQL还能够操作其它类型的数据库
// 告诉Sequelize当前要操作的数据库类型
dialect: 'mysql',
// 连接池
pool: {
// 最多有多少个连接
max: 5,
// 最少有多少个连接
min: 0,
// 当前连接多久没有操作就断开
idle: 10000,
// 多久没有获取到连接就断开
acquire: 30000,
}
});
// 3.创建模型
const Student = sequelize.define('student', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
// varchar(255)
type: Sequelize.STRING,
allowNull: false,
unique: true
}
}, {
// 告诉sequelize不需要自动将表名变成复数
freezeTableName: true,
// 不需要自动创建createAt/updateAt这两个字段
timestamps: false
});
const Teacher = sequelize.define('teacher', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
// varchar(255)
type: Sequelize.STRING,
allowNull: false,
unique: true
}
}, {
// 告诉sequelize不需要自动将表名变成复数
freezeTableName: true,
// 不需要自动创建createAt/updateAt这两个字段
timestamps: false
});
const Relation = sequelize.define('relation', {
studentId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Student,
key: 'id'
}
},
teacherId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Teacher,
key: 'id'
}
}
}, {
// 告诉sequelize不需要自动将表名变成复数
freezeTableName: true,
// 不需要自动创建createAt/updateAt这两个字段
timestamps: false
});
// 4.建立查询关系
// 一个学生属于多个老师
Student.belongsToMany(Teacher, {
through: Relation
});
// 一个老师属于多个学生
Teacher.belongsToMany(Student, {
through: Relation
});
sequelize.sync();
})();
往不同的表当中,分别添加对应的测试数据:
student
INSERT INTO `demo`.`student` (`id`, `name`) VALUES (1, 'zs');
INSERT INTO `demo`.`student` (`id`, `name`) VALUES (2, 'ls');
INSERT INTO `demo`.`student` (`id`, `name`) VALUES (3, 'ww');
teacher
INSERT INTO `demo`.`teacher` (`id`, `name`) VALUES (1, '张三');
INSERT INTO `demo`.`teacher` (`id`, `name`) VALUES (2, '李四');
INSERT INTO `demo`.`teacher` (`id`, `name`) VALUES (3, '王五');
relation
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (1, 1);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (1, 2);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (1, 3);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (2, 1);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (2, 2);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (2, 3);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (3, 1);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (3, 2);
INSERT INTO `demo`.`relation` (`studentId`, `teacherId`) VALUES (3, 3);
关联查询
当我们查询学生的时候可以将当前所查询的学生所属于哪个老师给一起查询出来:
// 1.导入Sequelize
const Sequelize = require('sequelize');
(async () => {
// 2.配置连接信息
const sequelize = new Sequelize('demo', 'root', 'yangbuyiya', {
// MySQL服务器地址
host: 'www.yangbuyi.top',
// MySQL服务器端口号
port: 3310,
// 注意点: Sequelize不仅仅能操作MySQL还能够操作其它类型的数据库
// 告诉Sequelize当前要操作的数据库类型
dialect: 'mysql',
// 连接池
pool: {
// 最多有多少个连接
max: 5,
// 最少有多少个连接
min: 0,
// 当前连接多久没有操作就断开
idle: 10000,
// 多久没有获取到连接就断开
acquire: 30000,
}
});
// 3.创建模型
const Student = sequelize.define('student', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
// varchar(255)
type: Sequelize.STRING,
allowNull: false,
unique: true
}
}, {
// 告诉sequelize不需要自动将表名变成复数
freezeTableName: true,
// 不需要自动创建createAt/updateAt这两个字段
timestamps: false
});
const Teacher = sequelize.define('teacher', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
// varchar(255)
type: Sequelize.STRING,
allowNull: false,
unique: true
}
}, {
// 告诉sequelize不需要自动将表名变成复数
freezeTableName: true,
// 不需要自动创建createAt/updateAt这两个字段
timestamps: false
});
const Relation = sequelize.define('relation', {
studentId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Student,
key: 'id'
}
},
teacherId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: Teacher,
key: 'id'
}
}
}, {
// 告诉sequelize不需要自动将表名变成复数
freezeTableName: true,
// 不需要自动创建createAt/updateAt这两个字段
timestamps: false
});
// 4.建立查询关系
// 一个学生属于多个老师
Student.belongsToMany(Teacher, {
through: Relation
});
// 一个老师属于多个学生
Teacher.belongsToMany(Student, {
through: Relation
});
sequelize.sync();
const student = await Student.findOne({
where: {
id: 1
},
include: {
model: Teacher
}
});
console.log(student.dataValues.teachers.map(t => t.dataValues.name));
})();
查询老师也可以将该老师所属于哪些学生也可以带出来:
const teacher = await Teacher.findOne({
where: {
id: 1
},
include: {
model: Student
}
});
console.log(teacher.dataValues.students.map(s => s.dataValues.name));