image.png

4-1 创建菜单和角色model

创建RoleAccess model

src/db/models/roleAccess.ts

  1. import { DataTypes, Model, Optional } from 'sequelize'
  2. import seq from '../seq'
  3. export interface RoleAccessModelProps {
  4. id: number;
  5. role_id: number;
  6. access_id: number;
  7. }
  8. interface RoleAccessCreationAttributes extends Optional<RoleAccessModelProps, "id"> {}
  9. interface RoleAccessInstance extends Model<RoleAccessModelProps, RoleAccessCreationAttributes>, RoleAccessModelProps {}
  10. const RoleAccess = seq.define<RoleAccessInstance>('RoleAccess', {
  11. id: {
  12. primaryKey: true,
  13. type: DataTypes.INTEGER,
  14. autoIncrement: true
  15. },
  16. role_id: {
  17. type: DataTypes.INTEGER.UNSIGNED,
  18. comment: '外键 关联roles表id'
  19. },
  20. access_id: {
  21. type: DataTypes.INTEGER,
  22. comment: '外键 关联access表id'
  23. }
  24. })
  25. export default RoleAccess

4-2 创建用户和角色model

创建userRole model

src/db/models/userRole.ts

  1. import { DataTypes, Model, Optional } from 'sequelize'
  2. import seq from '../seq'
  3. export interface UserRoleModelProps {
  4. id: number;
  5. user_id: number;
  6. role_id: number;
  7. }
  8. interface UserRoleCreationAttributes extends Optional<UserRoleModelProps, "id"> {}
  9. interface UserRoleInstance extends Model<UserRoleModelProps, UserRoleCreationAttributes>, UserRoleModelProps {}
  10. const UserRole = seq.define<UserRoleInstance>('UserRole', {
  11. id: {
  12. primaryKey: true,
  13. type: DataTypes.INTEGER,
  14. autoIncrement: true
  15. },
  16. user_id: {
  17. type: DataTypes.INTEGER.UNSIGNED,
  18. comment: '外键 关联user表id'
  19. },
  20. role_id: {
  21. type: DataTypes.INTEGER.UNSIGNED,
  22. comment: '外键 关联roles表id'
  23. }
  24. })
  25. export default UserRole

4-3 建立关联

src/db/models/index.ts

  1. import UserModel from './user'
  2. import AccessModel from './access'
  3. import RolesModel from './roles'
  4. import RoleAccessModel from './roleAccess'
  5. import UserRoleModel from './userRole'
  6. // 外键关联 建立从属关系
  7. ;(() => {
  8. /**
  9. * onDelete onUpdate
  10. * 官方配置文档 https://www.sequelize.com.cn/core-concepts/assocs#ondelete-%E5%92%8C-onupdate
  11. * 配置说明 https://blog.csdn.net/yajing8/article/details/73014004
  12. */
  13. // RoleMenuModel.role_id = UserModel.id
  14. RoleAccessModel.belongsTo(RolesModel, {
  15. // 父表delete、update的时候,子表会delete、update掉关联记录;
  16. onDelete: 'CASCADE',
  17. onUpdate: 'CASCADE',
  18. foreignKey: 'role_id'
  19. })
  20. // RoleMenuModel.role_id = AccessModel.id
  21. RoleAccessModel.belongsTo(AccessModel, {
  22. onDelete: 'CASCADE',
  23. onUpdate: 'CASCADE',
  24. foreignKey: 'access_id'
  25. })
  26. // 双向关联 有利于双向联表查询
  27. RolesModel.hasMany(RoleAccessModel, {
  28. onDelete: 'CASCADE',
  29. onUpdate: 'CASCADE',
  30. foreignKey: 'role_id'
  31. })
  32. AccessModel.hasMany(RoleAccessModel, {
  33. onDelete: 'CASCADE',
  34. onUpdate: 'CASCADE',
  35. foreignKey: 'access_id'
  36. })
  37. // UserRoleModel.role_id = RolesModel.id
  38. UserRoleModel.belongsTo(RolesModel, {
  39. onDelete: 'CASCADE',
  40. onUpdate: 'CASCADE',
  41. foreignKey: 'role_id'
  42. })
  43. // UserRoleModel.role_id = UserModel.id
  44. UserRoleModel.belongsTo(UserModel, {
  45. onDelete: 'CASCADE',
  46. onUpdate: 'CASCADE',
  47. foreignKey: 'user_id'
  48. })
  49. // 双向关联 有利于双向联表查询
  50. RolesModel.hasMany(UserRoleModel, {
  51. onDelete: 'CASCADE',
  52. onUpdate: 'CASCADE',
  53. foreignKey: 'role_id'
  54. })
  55. UserModel.hasMany(UserRoleModel, {
  56. onDelete: 'CASCADE',
  57. onUpdate: 'CASCADE',
  58. foreignKey: 'user_id'
  59. })
  60. })()
  61. export {
  62. UserModel,
  63. AccessModel,
  64. RolesModel,
  65. RoleAccessModel,
  66. UserRoleModel
  67. }

4-4 更新同步model

建立关联后 关联代码可以注释掉 关联一次就行了

  1. npm run db

注意表关联

关联后,父表id删除后 我们也想同时自动删除关联表中数据 需要改下表配置 on Delete 为 cascade

说明文档 外键关联设置 on Delete on Update
https://blog.csdn.net/yajing8/article/details/73014004

方式一 表关联时代码中设置

文档地址:https://www.sequelize.com.cn/core-concepts/assocs#ondelete-%E5%92%8C-onupdate

方式二 直接改表配置

image.png

目前最新源码

https://gitee.com/brolly/vue3-admin-server