javascript - Sequelize "where"指的是包含模型列

标签 javascript sql node.js include sequelize.js

我有以下使用 node.js 和 Sequelize (@3.3.2) 的模型:

module.exports = function (sequelize, DataTypes) {
    var Topic = sequelize.define("Topic", {
        name: DataTypes.STRING,
    }, {
        classMethods: {
            associate: function (models) {
                Topic.hasMany(models.Section, {as: 'sections'});
            }
        }
    });

    return Topic;
};

module.exports = function (sequelize, DataTypes) {
    var Section = sequelize.define("Section", {
        previewImg: DataTypes.STRING,
        description: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        classMethods: {
            associate: function (models) {
                Section.belongsTo(models.Topic);
                Section.belongsToMany(models.Domain, {through: 'SectionDomainsExclusive', as: 'exclusiveDomains'});
            }
        }
    });

    return Section;
};

module.exports = function (sequelize, DataTypes) {
    var Domain = sequelize.define("Domain", {
        name: {
            type: DataTypes.STRING,
            allowNull: false
        }
    });

    return Domain;
};

所以一个主题有很多部分,一个部分可以有一个“独占”域的列表。 所需的功能是:

If there are no exclusive domains (no relation), the domain is visible in all these domains. If there are one or more exclusive domains assigned, the section shall be visible to only these domains

现在我想检索主题以及可见的部分:

models.Topic.findAll({
        include: [{
            model: models.Section,
            as: 'sections',
            include: [{
                model: models.Domain,
                required: false,
                as: 'exclusiveDomains',
            }],
            where: {
                'exclusiveDomains.id': {
                    $or: {
                        $eq: 3,
                        $eq: null
                    }
                }
            }
        }]
    }).then(function (objList) {
        ///...
    });

所以我想在域上使用别名“exclusiveDomains”进行 LEFT JOIN(必需:false),然后在 WHERE 整个查询结果中没有找到独占域(NULL)或者有一个部分是域 3 可见,这是我的域。

我不能将 WHERE 部分放在包含中,因为这只会限制连接的结果,如果该部分没有独占域列表或者我的域 (3) 不在列表中,我无法区分.

不幸的是,Sequelize 总是放置“section”。在我的 WHERE 前面,所以这会导致以下错误:

Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: sections.exclusiveDomains.id

SQL 字符串应该是这样的:

SELECT `Topic`.`id`,
      `sections`.`id`                                                 AS
        `sections.id`,
      `sections.exclusiveDomains`.`id`                                AS
        `sections.exclusiveDomains.id`,
      `sections.exclusiveDomains.SectionDomainsExclusive`.`sectionid` AS
        `sections.exclusiveDomains.SectionDomainsExclusive.SectionId`,
      `sections.exclusiveDomains.SectionDomainsExclusive`.`domainid`  AS
        `sections.exclusiveDomains.SectionDomainsExclusive.DomainId`
    FROM   `topics` AS `Topic`
      INNER JOIN `sections` AS `sections`
        ON `Topic`.`id` = `sections`.`topicid`
      INNER JOIN `sectionusergroup` AS `sections.usergroups.SectionUsergroup`
        ON `sections`.`id` = `sections.usergroups.SectionUsergroup`.`sectionid`
      LEFT OUTER JOIN `sectiondomainsexclusive` AS `sections.exclusiveDomains.SectionDomainsExclusive`
        ON `sections`.`id` = `sections.exclusiveDomains.SectionDomainsExclusive`.`sectionid`
      LEFT OUTER JOIN `domains` AS `sections.exclusiveDomains`
        ON `sections.exclusiveDomains`.`id` = `sections.exclusiveDomains.SectionDomainsExclusive`.`domainid`
    WHERE (`sections.exclusiveDomains`.`id` = 3 OR `sections.exclusiveDomains`.`id` IS NULL);

不幸的是我无法获取sections.exclusiveDomains.id部分,因为Sequelize总是把section放在前面,所以它看起来像sectionsexclusiveDomains.id,这显然是错误的。

我能做什么?

感谢您的支持!

最佳答案

我发现原始查询是最好的方法。

where:["sections.exclusiveDomains.id = 3 OR sections.exclusiveDomains.id is null", null]

可能有一种更简洁的方法来提取完全限定的名称,但就目前而言,这似乎是一种享受!

关于javascript - Sequelize "where"指的是包含模型列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936166/

相关文章:

javascript - 我如何知道浏览器中是否已关闭 Javascript?

javascript - Angular + Selenium : Cannot get value from input field

php - 选择在日期范围内登录的 MySql 行,返回每天的小时数

css - Node compile.js 不生成组合的 css 文件

Node.js/了解 ReadStream.pipe 是否正在传输数据

javascript - 单击时显示 DataTables 搜索栏

javascript - IE 中滚动高度不正确

mysql - SQL 与 MySQL : Rules about aggregate operations and GROUP BY

mysql - MySQL中关于 'left join'的问题

node.js - 我的服务器(node.js)项目有一个配置文件,需要在我的客户端(react)中使用它