我提前为这篇冗长的帖子道歉!

我试图在nodeJs中使用sequelize在嵌套的include中使用 required: true 查询Wordpress mysql数据库 .

但是,生成的查询包含一个错误的连接(我希望连接像嵌套的where子句一样嵌套) . 我似乎无法弄清楚我是否错误地配置了我的架构,或者我是否只是在做其他愚蠢的事情 .

任何帮助将不胜感激!

从本质上讲,我的架构是:

const Post = sequelize.define('wp_posts', {
  ID: {
    type: DataType.BIGINT,
    primaryKey: true
  }
});

const TermRelationship = sequelize.define('wp_term_relationships', {
  object_id: {
    type: DataType.BIGINT,
    primaryKey: true,
    allowNull: false
  },
  term_taxonomy_id: {
    type: DataType.BIGINT,
    primaryKey: true,
    allowNull: false
  }
});

const TermTaxonomy = sequelize.define('wp_term_taxonomy', {
  term_taxonomy_id: {
    type: DataType.BIGINT,
    primaryKey: true
  }
});

const Term = sequelize.define('wp_terms', {
  term_id: {
    type: DataType.BIGINT,
    primaryKey: true
  }
});

我定义的关系是:

Post.belongsToMany(TermTaxonomy, {
  through: TermRelationship,
  otherKey: 'term_taxonomy_id',
  foreignKey: 'object_id',
  as: 'termTaxonomies'
});

TermTaxonomy.belongsTo(Term, {
  foreignKey: 'term_id',
  as: 'term'
});

我正在执行的查询是

const query = {
  limit: 1,
  include: [
    {
      model: TermTaxonomy,
      required: true,
      as: 'termTaxonomies',
      include: [
        {
          model: Term,
          as: 'term',
          required: true,
        }
      ]
    }
  ]
};

但是,生成的查询包括错误的连接 . 这是生成的查询 . 我在评论中包含了错误:

SELECT
    `wp_posts`.*,
    `termTaxonomies`.`term_taxonomy_id` AS `termTaxonomies.term_taxonomy_id`,
    `termTaxonomies`.`term_id` AS `termTaxonomies.term_id`,
    `termTaxonomies`.`taxonomy` AS `termTaxonomies.taxonomy`,
    `termTaxonomies`.`description` AS `termTaxonomies.description`,
    `termTaxonomies`.`parent` AS `termTaxonomies.parent`,
    `termTaxonomies`.`count` AS `termTaxonomies.count`,
    `termTaxonomies->wp_term_relationships`.`object_id` AS `termTaxonomies.wp_term_relationships.object_id`,
    `termTaxonomies->wp_term_relationships`.`term_taxonomy_id` AS `termTaxonomies.wp_term_relationships.term_taxonomy_id`,
    `termTaxonomies->wp_term_relationships`.`term_order` AS `termTaxonomies.wp_term_relationships.term_order`
FROM
    (
        SELECT
            `wp_posts`.`ID`,
            `wp_posts`.`post_author`,
            `wp_posts`.`post_date_gmt`,
            `wp_posts`.`post_content`,
            `wp_posts`.`post_title`,
            `wp_posts`.`post_excerpt`,
            `wp_posts`.`post_status`,
            `wp_posts`.`comment_status`,
            `wp_posts`.`ping_status`,
            `wp_posts`.`post_password`,
            `wp_posts`.`post_name`,
            `wp_posts`.`to_ping`,
            `wp_posts`.`pinged`,
            `wp_posts`.`post_modified_gmt`,
            `wp_posts`.`post_content_filtered`,
            `wp_posts`.`post_parent`,
            `wp_posts`.`guid`,
            `wp_posts`.`menu_order`,
            `wp_posts`.`post_type`,
            `wp_posts`.`post_mime_type`,
            `wp_posts`.`comment_count`,
            -- ERROR
            -- wp_terms cannot be joined to wp_posts
            `termTaxonomies->term`.`term_id` AS `termTaxonomies.term.term_id`,
            `termTaxonomies->term`.`name` AS `termTaxonomies.term.name`,
            `termTaxonomies->term`.`slug` AS `termTaxonomies.term.slug`,
            `termTaxonomies->term`.`term_group` AS `termTaxonomies.term.term_group`
        FROM
            `wp_posts` AS `wp_posts`
            -- ERROR: bad join!
            -- wp_terms cannot be joined to wp_posts
            INNER JOIN `wp_terms` AS `termTaxonomies->term` ON `termTaxonomies`.`term_id` = `termTaxonomies->term`.`term_id`
        WHERE
            (
                SELECT
                    `wp_term_relationships`.`object_id`
                FROM
                    `wp_term_relationships` AS `wp_term_relationships`
                    INNER JOIN `wp_term_taxonomy` AS `wp_term_taxonomy` ON `wp_term_relationships`.`term_taxonomy_id` = `wp_term_taxonomy`.`term_taxonomy_id`
                    INNER JOIN `wp_terms` AS `wp_term_taxonomy->term` ON `wp_term_taxonomy`.`term_id` = `wp_term_taxonomy->term`.`term_id`
                WHERE
                    (
                        `wp_posts`.`ID` = `wp_term_relationships`.`object_id`
                    )
                LIMIT
                    1
            ) IS NOT NULL
        LIMIT
            1
    ) AS `wp_posts`
    INNER JOIN (
        `wp_term_relationships` AS `termTaxonomies->wp_term_relationships`
        INNER JOIN `wp_term_taxonomy` AS `termTaxonomies` ON `termTaxonomies`.`term_taxonomy_id` = `termTaxonomies->wp_term_relationships`.`term_taxonomy_id`
    ) ON `wp_posts`.`ID` = `termTaxonomies->wp_term_relationships`.`object_id`;

由于连接不正确,我得到的错误是 'Unknown column 'termTaxonomies.term_id' in 'on clause' .

如果我删除 required: truelimit 选项,则生成的查询有效,因为它不再执行奇怪的内连接 . 但是,我似乎无法使用所需的嵌套include来实现此功能 .

仅供参考:我使用的是sequelize 4.37.10和1.5.3 mysql2 1.5.3 .

非常感谢你!