Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

node.js - NodeJS Sequelize error: Column 'id' in field list is ambiguous

Model define

// 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',
      });
    }
  };
}

Now I am getting the follwing error, Column 'id' in field list is ambiguous. how can i uniquely grab the correct ID from this statment

Error Desc:

code: 'ER_NON_UNIQ_ERROR', errno: 1052, sqlState: '23000', sqlMessage: "Column 'id' in field list is ambiguous", 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;', parameters: undefined },

      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' ],
          },
        ],
      });

Please help me, thx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...