Node.js 使用Sequelize在多对多关联中遇到的令人惊讶的问题
我创建了三个表post、tags和post_标记。每个帖子通过PostTags属于多个标签,每个标签通过PostTag属于多个帖子。令人惊讶的问题是,当我将posts.id排序为Node.js 使用Sequelize在多对多关联中遇到的令人惊讶的问题,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我创建了三个表post、tags和post_标记。每个帖子通过PostTags属于多个标签,每个标签通过PostTag属于多个帖子。令人惊讶的问题是,当我将posts.id排序为DESC时,标记将为空,并且连接查询结果不会返回任何标记。但是当我将id排序为ASC时,标记将显示在结果JSON中。有些帖子没有标签。我不知道为什么会有这个问题。如果知道数据库的类型有助于解决问题,我将使用Postgresql 模型结构为: // Posts Model const posts = (sequelize,
DESC
时,标记将为空,并且连接查询结果不会返回任何标记。但是当我将id
排序为ASC
时,标记将显示在结果JSON中。有些帖子没有标签。我不知道为什么会有这个问题。如果知道数据库的类型有助于解决问题,我将使用Postgresql
模型结构为:
// 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;
};
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']]
});
我通过以下选项选择了行:
// 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;
};
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']]
});
您是否将sequelize查询记录到console?我从未对此有过任何异议,但您可能会在查询中得到您不期望的结果。。。如果您记录它们,请将它们粘贴到您的问题中…是的,控制台中没有任何错误!我截断表,再次插入post和标记!现在问题解决了!我不知道为什么!:-|我不是故意犯错的。。。sequelize将输出它在开发中调用的所有查询。。。当我发现并启用它时,在排除爬行的奇怪东西时非常有用。查询日志现在在控制台中打印。但是我测试了许多在表中插入数据的类型,最后一个问题不存在!因此,现在我无法显示查询输出。如果问题再次出现,我会告诉你。好东西,很高兴你能成功。。。干杯