Mysql 使用事务和limiit对findAll查询进行续集,并抵消其不起作用
我试图在sequelize上使用事务添加分页,但在添加限制和偏移量时,它显示了一个错误SequelizeDatabaseError:无效的列名“typeId”Mysql 使用事务和limiit对findAll查询进行续集,并抵消其不起作用,mysql,sql,node.js,sequelize.js,sequelize-typescript,Mysql,Sql,Node.js,Sequelize.js,Sequelize Typescript,我试图在sequelize上使用事务添加分页,但在添加限制和偏移量时,它显示了一个错误SequelizeDatabaseError:无效的列名“typeId” try { await sequelize.transaction(async (transaction) => { data = await Test.findAll({ order: [['id', 'DESC']], where: { type: 'Y' },
try {
await sequelize.transaction(async (transaction) => {
data = await Test.findAll({
order: [['id', 'DESC']],
where: { type: 'Y' },
attributes: ['id', 'type', 'name'],
include: [
{
model: xyz,
as: 'xyz',
attributes: ['id'],
include: {
model: efg,
as: 'efg',
attributes: ['id', 'typeId'],
},
},
{
model: abc,
as: 'abc',
attributes: ['id'],
include: {
model: ijk,
as: 'ijk',
attributes: ['id', 'name'],
},
}
],
transaction,
limit: 10,
offset: 0,
}).then(res=>{
return res;
})
});
} catch(e) {
}
任何建议都将不胜感激。谢谢 您需要将事务作为第二个参数,如下所示-
try {
await sequelize.transaction(async (transaction) => {
data = await Test.findAll({
order: [['id', 'DESC']],
where: { type: 'Y' },
attributes: ['id', 'type', 'name'],
include: [
{
model: xyz,
as: 'xyz',
attributes: ['id'],
include: {
model: efg,
as: 'efg',
attributes: ['id', 'typeId'],
},
},
{
model: abc,
as: 'abc',
attributes: ['id'],
include: {
model: ijk,
as: 'ijk',
attributes: ['id', 'name'],
},
}
],
// comment the line below
// transaction,
limit: 10,
offset: 0,
},
{ transaction }
).then(res=>{
return res;
})
});
} catch(e) {
}
我知道它描述了如何以您实现的方式执行
findAll
方法的事务,但在我尝试的情况下,它并没有像预期的那样工作。您需要将事务作为第二个参数,如下所示-
try {
await sequelize.transaction(async (transaction) => {
data = await Test.findAll({
order: [['id', 'DESC']],
where: { type: 'Y' },
attributes: ['id', 'type', 'name'],
include: [
{
model: xyz,
as: 'xyz',
attributes: ['id'],
include: {
model: efg,
as: 'efg',
attributes: ['id', 'typeId'],
},
},
{
model: abc,
as: 'abc',
attributes: ['id'],
include: {
model: ijk,
as: 'ijk',
attributes: ['id', 'name'],
},
}
],
// comment the line below
// transaction,
limit: 10,
offset: 0,
},
{ transaction }
).then(res=>{
return res;
})
});
} catch(e) {
}
我知道,它描述了以您实现的方式执行
findAll
方法的事务,但在我尝试的情况下,它并没有像预期的那样工作。typeId字段出现在efg表中。您是否看到start transaction
带有一些uuid。如果是这样的话,事务处理工作正常。检查你的专栏是否在那里。或者更具体地说,如果您已经定义了列ther.typeId字段,请检查您的模型是否存在于efg表中。您是否看到带有某些uuid的启动事务
。如果是这样的话,事务处理工作正常。检查你的专栏是否在那里。或者更具体地说,如果您已经定义了列ther,请检查您的模型。我尝试将上面的代码作为第二个参数传递事务,但仍然无法显示相同的错误无效列类型我认为,您的问题与事务无关,我建议您暂时从sequelize
语句中删除typeId
,并检查它是否成功执行。是的,我也尝试过这样做,但它显示了另一个错误,即SequelizeDatabaseError:在order by列表中多次指定了列。“按顺序排列”列表中的列必须是唯一的。我认为您没有以正确的方式框定问题。我请求您发布sequelize语句中使用的模型,以及控制台上出现的错误。我还建议您先形成一个sequelize语句,然后考虑将其转换为事务。@Sujeet Agrahari您能与我分享您的代码吗?我会检查一下它是否有效。我尝试将上述代码作为第二个参数传递给事务,但仍然不起作用,显示相同的错误无效列类型我认为,您的问题与事务无关,我建议您暂时从sequelize
语句中删除typeId
,并检查该语句是否成功执行。是的,我也尝试过这样做,但它显示了另一个错误,即SequelizeDatabaseError:在order by列表中多次指定了列。“按顺序排列”列表中的列必须是唯一的。我认为您没有以正确的方式框定问题。我请求您发布sequelize语句中使用的模型,以及控制台上出现的错误。我还建议您先形成sequelize语句,然后考虑将其转换为transaction。@Sujeet Agrahari您能与我分享您的代码吗?我会检查一下它是否有效