简体   繁体   English

如何在 sequelize findAll 中包含特定关联?

[英]How to include a specific association in sequelize findAll?

I've created a database with two tables, Users and Points.我创建了一个包含两个表,用户和点的数据库。 A user can have many points and a point stores the ID of the user who sent it and the user who received it.一个用户可以有很多点,一个点存储发送它的用户和接收它的用户的ID。 I am trying to query for a table grouped by user showing the sum of the amounts of all their points, which is working querying raw in postgresql but not in sequelize.我正在尝试查询按用户分组的表,显示所有点的总和,该表在 postgresql 中查询原始数据,但不在 sequelize 中查询。

Working in postgresql:在 postgresql 中工作:

PostgreSQL 查询

Creating the models with sequelize:使用 sequelize 创建模型:

User.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    telegram_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      unique: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  },
  {
    tableName: "users",
    sequelize: sequelize, // this bit is important
  }
);

Point.init(
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      amount: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      to_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      from_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
    },
    {
      tableName: "points",
      sequelize: sequelize, // this bit is important
    }
  );

User.hasMany(Point, {
  sourceKey: "telegram_id",
  foreignKey: "to_id",
  as: "recievedPoints", // this determines the name in `associations`!
});

User.hasMany(Point, {
  sourceKey: "telegram_id",
  foreignKey: "from_id",
  as: "sentPoints", // this determines the name in `associations`!
});

Point.belongsTo(User, {
  foreignKey: "to_id",
  targetKey: "telegram_id",
  as: "toUser",
});
Point.belongsTo(User, {
  foreignKey: "from_id",
  targetKey: "telegram_id",
  as: "fromUser",
});

Attempting to make same query with sequelize:尝试使用 sequelize 进行相同的查询:

  const points = await Point.findAll({
    attributes: [
      "users.name",
      "points.to_id",
      [Sequelize.fn("SUM", Sequelize.col("points.amount")), "points.amount"],
    ],
    include: ["toUser"],
    group: ["users.name", "points.to_id"],
  });

Resulting error:结果错误:

SequelizeDatabaseError: invalid reference to FROM-clause entry for table "users" SequelizeDatabaseError:对表“users”的 FROM 子句条目的引用无效

SQL generated by sequelize: sequelize 生成的 SQL:

SELECT "users"."name", "points"."to_id", SUM("points"."amount") AS "points.amount", "toUser"."id" AS "toUser.id", "toUser"."telegram_id" AS "toUser.telegram_id", "toUser"."name" AS "toUser.name", "toUser"."createdAt" AS "toUser.createdAt", "toUser"."updatedAt" AS "toUser.updatedAt" FROM "points" AS "Point" 
LEFT OUTER JOIN "users" AS "toUser" ON "Point"."to_id" = "toUser"."telegram_id" GROUP BY "users"."name", "points"."to_id";

RAW QUERY :原始查询:

SELECT "users"."name", "points"."to_id", SUM("points"."amount") AS "points.amount", "toUser"."id" AS "toUser.id", "toUser"."telegram_id" AS "toUser.telegram_id", "toUser"."name" AS "toUser.name", "toUser"."createdAt" AS "toUser.createdAt", "toUser"."updatedAt" AS "toUser.updatedAt" 
FROM "points" AS "Point" 
LEFT OUTER JOIN "users" AS "toUser" ON "Point"."to_id" = "toUser"."telegram_id" GROUP BY "users"."name", "points"."to_id";

As per your raw query :根据您的原始查询:

Change "users" to "toUser" every where在任何地方将“用户”更改为“toUser”

Change "points" to "Point" every where , like this :“点”更改为“点”无处不在,如下所示:

const points = await Point.findAll({
    attributes: [
        "toUser.name",
        "Point.to_id",
        [Sequelize.fn("SUM", Sequelize.col("Point.amount")), "Point.amount"],
    ],
    include: ["toUser"],
    group: ["toUser.name", "Point.to_id"],
});

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

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