我的目标是进行多方查询,现在我知道如何使用sql来实现它,代码如下,但是我不知道如何使用续集来实现它。
续集4.42.0 mysql2 1.6.4
课程模式:
'use strict';
module.exports = (sequelize, DataTypes) => {
const course = sequelize.define('Course', {
name: {
allowNull: false,
type: DataTypes.STRING
},
description: {
type: DataTypes.TEXT
},
thumbnail: {
type: DataTypes.STRING
},
program: {
type: DataTypes.STRING
},
free: {
allowNull: false,
type: DataTypes.TINYINT
},
state: {
allowNull: false,
type: DataTypes.TINYINT
}
}, {
tableName: 't_course'
});
course.associate = function (models) {
// 多对多关系映射
models.Course.belongsToMany( models.Organization, {
through: models.Organization_Course
});
};
return course;
};
组织模型:
'use strict';
module.exports = (sequelize, DataTypes) => {
const organization = sequelize.define('Organization', {
name: {
allowNull: false,
type: DataTypes.STRING
},
type: {
allowNull: false,
type: DataTypes.ENUM,
values: ['school', 'agency']
}
}, {
tableName: 't_organization',
}
);
organization.associate = function (models) {
models.Organization.belongsTo(models.District);
// 多对多关系映射
models.Organization.belongsToMany( models.Course, {
through: models.Organization_Course
});
models.Organization.hasMany(models.User);
};
return organization;
};
Organization_Course模型:
'use strict';
module.exports = (sequelize, DataTypes) => {
const organization_course = sequelize.define('Organization_Course', {}, {
tableName: 'r_organization_course',
}
);
// organization_course.associate = function (models) {
// };
return organization_course;
};
mysql代码:
SELECT `Course`.`id`, `Course`.`name`, `Course`.`description`, `Course`.`thumbnail`, `Course`.`program`
, `Course`.`free`, `Course`.`state`, `Course`.`created_at`, `Course`.`updated_at`, `Course`.`deleted_at`
, `Organizations`.`id` AS `Organizations.id`, `Organizations`.`name` AS `Organizations.name`, `Organizations`.`type` AS `Organizations.type`, `Organizations`.`created_at` AS `Organizations.created_at`, `Organizations`.`updated_at` AS `Organizations.updated_at`
, `Organizations`.`deleted_at` AS `Organizations.deleted_at`, `Organizations`.`district_id` AS `Organizations.district_id`, `Organizations->Organization_Course`.`created_at` AS `Organizations.Organization_Course.created_at`, `Organizations->Organization_Course`.`updated_at` AS `Organizations.Organization_Course.updated_at`, `Organizations->Organization_Course`.`deleted_at` AS `Organizations.Organization_Course.deleted_at`
, `Organizations->Organization_Course`.`course_id` AS `Organizations.Organization_Course.course_id`, `Organizations->Organization_Course`.`organization_id` AS `Organizations.Organization_Course.organization_id`
FROM `t_course` `Course`
INNER JOIN (`r_organization_course` `Organizations->Organization_Course`
INNER JOIN `t_organization` `Organizations`
ON `Organizations`.`id` = `Organizations->Organization_Course`.`organization_id`
AND (`Organizations->Organization_Course`.`deleted_at` > '2019-04-12 21:18:04'
OR `Organizations->Organization_Course`.`deleted_at` IS NULL))
ON `Course`.`id` = `Organizations->Organization_Course`.`course_id`
AND ((`Organizations`.`deleted_at` > '2019-04-12 21:18:04'
OR `Organizations`.`deleted_at` IS NULL)
AND (`Organizations`.`id` = 3 or `Course`.`free` = 1))
WHERE (`Course`.`deleted_at` > '2019-04-12 21:18:04'
OR `Course`.`deleted_at` IS NULL);
最重要的部分:AND(Organizations
。id
= 3或Course
。free
= 1))
我不知道如何用续集来实现它。