我有一个名为 tests
的 postgres 表,其中包含很少的记录,现在我想获取该表的所有记录但无法获取,因为它只提供了 id、createdAt 和 updatedAt
。
所以要么我必须提供一个对象,其中包含我不想要的列名,我希望它应该是动态的,所以在此之后如果我传递另一个表名,它将提供该数据。 我试过了,但它返回空对象数组
Project.findAll(attributes: ['*']
}).then(function(project) {
console.log("select_data: " + JSON.stringify(project));
})
最佳答案
我不是很清楚这里的问题是什么,但听起来好像在定义模型时,您可能没有在模型定义中包含表的所有列名。它应该是这样的:
module.exports = function(sequelize, DataTypes) {
const test = sequelize.define('test', {
my_column: DataTypes.STRING,
my_other_column: DataTypes.STRING,
my_boolean_column: DataTypes.BOOLEAN,
}, {
timestamps: true,
underscored: true,
tableName: 'tests',
})
return test
}
请注意表中的每一列都在模型中明确定义。
关于javascript - 如何使用sequelize获取表的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43556508/