Sequelize-关联查询下
关于本文主要的内容为多对多表之间的关联查询,如何进行绑定表之间的关系的,一对一,一对多,在前面的文章当中上和中文章当中都已经介绍过了,本文就详细的来带大家来看看多对多的玩法首先需要创建三张表,以及对应的测试数据,创建模型代码如下:
// 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));
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具