【问题标题】:NodeJS Sequelize error: Column 'id' in field list is ambiguousNodeJS Sequelize 错误:字段列表中的列“id”不明确
【发布时间】:2021-09-14 14:45:47
【问题描述】:

模型定义

// Stock
export default function(app: Application) {
  const { STRING, BIGINT, TIME, TINYINT } = app.Sequelize;

  const Stock = app.model.define('stock', {
    id: {
      type: BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    uid: {
      type: BIGINT,
    },
    aid: {
      type: BIGINT,
    },
    symbol: {
      type: STRING,
      allowNull: false,
    },
    name: {
      type: STRING,
      comment: 'name',
    },
    ....
  });

  return class extends Stock {};
}
// TransactionRecord
export default function(app: Application) {
  const { STRING, BIGINT, TIME, TINYINT, INTEGER, DECIMAL } = app.Sequelize;

  const TransactionRecord = app.model.define('transaction_record', {
    id: {
      type: BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    uid: {
      type: BIGINT,
    },
    aid: {
      type: BIGINT,
    },
    sid: {
      type: BIGINT,
    },
    ...
  });
  return class extends TransactionRecord {
    static associate() {
      app.model.TransactionRecord.belongsTo(app.model.Account, {
        as: 'account',
        foreignKey: 'aid',
      });
      app.model.TransactionRecord.belongsTo(app.model.User, {
        as: 'user',
        foreignKey: 'uid',
      });
      app.model.TransactionRecord.belongsTo(app.model.Stock, {
        as: 'stock',
        foreignKey: 'sid',
      });
    }
  };
}

现在我收到以下错误,字段列表中的列“id”不明确。我怎样才能从这个语句中唯一地获取正确的 ID

错误描述:

代码:'ER_NON_UNIQ_ERROR', 错误号:1052, sqlState: '23000', sqlMessage: "字段列表中的列 'id' 不明确", sql: 'SELECT count(DISTINCT(.`id`)) AS `count` FROM `transaction_record` AS LEFT OUTER JOIN stock AS stock ON .`id` = `stock`.`sid` LEFT OUTER JOIN `account` AS `account` ON .id = account.aid WHERE ``.uid = 1;', 参数:未定义},

      const offset = Math.max(Number(pageNo || 1) - 1, 0);
      const limit = Number(pageSize || 20);
      const result = await ctx.model.TransactionRecord.findAndCountAll({
        raw: true,
        distinct: true,
        offset: offset * limit,
        limit,
        where: {
          uid: Number(ctx.payload.id),
        },
        order: [
          [ 'updated', 'ASC' ],
        ],
        include: [
          {
            model: app.model.Stock,
            as: 'stock',
            attributes: [ 'symbol', 'name', 'market', 'currency' ],
          },
          {
            model: app.model.Account,
            as: 'account',
            attributes: [ 'name', 'super', 'currency' ],
          },
        ],
      });

请帮帮我,谢谢

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    如果您在两个表中都有相同的字段,那么就会出现这个错误。

    为此,您必须将字段名称与表名一起提供,以便知道该字段属于哪个表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2016-06-28
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      相关资源
      最近更新 更多