简体   繁体   English

使用 Sequelize 的多对多关联中令人惊讶的问题

[英]The surprising problem in Many-to-many associations using Sequelize

I created three tables posts, tags, and post_tags.我创建了三个表 post、tags 和 post_tags。 Each Post belongs to many Tags through PostTags and each Tags belongs to many Post through PostTag.每个帖子通过 PostTags 属于许多标签,每个标签通过 PostTag 属于许多帖子。 The surprising problem is when I sort posts.id as DESC the Tags will be empty and the join query result doesn't return any tag.令人惊讶的问题是,当我将posts.id排序为DESC ,标签将为空并且连接查询结果不返回任何标签。 but when I sort id as ASC the tags will be shown in the result JSON.但是当我将id排序为ASC ,标签将显示在结果 JSON 中。 Some of the posts don't have tag.有些帖子没有标签。 I don't know why this problem is there.我不知道为什么会出现这个问题。 If knowing the type of database helps to fix the problem, I use Postgresql.如果知道数据库的类型有助于解决问题,我使用 Postgresql。

Models structures are:模型结构是:

// Posts Model
const posts = (sequelize, DataTypes) => {
  const posts = sequelize.define(
    "posts",
    {
      userId: DataTypes.INTEGER,
      title: DataTypes.STRING,
    },
    {
      tableName: "posts",
      timestamps: true,
      paranoid: true
    }
  );

  // Relations
  posts.associate = (models) => {
    posts.belongsToMany(models.tags, {
      through: 'postTags',
      as: 'tags',
      foreignKey: 'postId',
      otherKey: 'tagId'
    });
  }

  return posts;
};

// Tags Model
const tags = sequelize.define(
    "tags",
    {
      title: DataTypes.STRING
    },
    {
      tableName: "tags",
      timestamps: true,
      paranoid: true
    }
  );

  // Relations
  tags.associate = (models) => {
    tags.belongsToMany(models.posts, {
      through: 'postTags',
      as: 'posts',
      foreignKey: 'tagId',
      otherKey: 'postId'
    });
  }

  return tags;
};

// PostTags Model
const post_tags = (sequelize, DataTypes) => {
  const post_tags = sequelize.define(
    "postTags",
    {
      postId: DataTypes.INTEGER,
      tagId: DataTypes.INTEGER
    },
    {
      tableName: "post_tags",
      timestamps: true,
      paranoid: true
    }
  );

  return post_tags;
};

And I selected rows by these options:我通过这些选项选择了行:

const posts = models.posts.findAll({
   attributes: [
     'id',
     'title'
   ],
   include: [
      {
         model: models.tags,
         as: 'tags',
         required: false,
         attributes: [
            'id',
            'title'
         ],
         through: {
            model: models.postTags,
            as: 'postTags',
            attributes: [
               'postId',
               'tagId'
            ]
         }
       }
   ],
   order: [['id', 'desc']]
 });

Are you logging your sequelize queries to console?您是否将 sequelize 查询记录到控制台? I have never had any issue with this, but you may be getting something you don't expect in your query... paste them both into your question if you log them...我从来没有遇到过任何问题,但是您可能会在查询中得到一些您不期望的东西……如果您记录它们,请将它们粘贴到您的问题中……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM