【发布时间】: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` ASLEFT OUTER JOINstockASstockON.`id` = `stock`.`sid` LEFT OUTER JOIN `account` AS `account` ON.id=account.aidWHERE ``.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