- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个关系数据库(Postgres),没有显式关系,即外键。但我正在使用 Sequelize 来查询数据库中的数据,并且我想通过急切加载 Sequelize 来查询数据。
模型
供应商
const Supplier = sequelize.define(
'Supplier',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
deleted: {
type: DataTypes.BOOLEAN,
defaultValue: false,
field: 'excluido',
},
createdAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
field: 'datacriacao',
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
field: 'dataalteracao',
},
socialName: {
type: DataTypes.STRING,
allowNull: false,
field: 'razaosocial',
},
logo: {
type: DataTypes.STRING,
allowNull: true,
field: 'logo',
},
},
{
underscored: true,
freezeTableName: true,
timestamps: false,
tableName: 'fornecedor',
},
);
供应商地址:
const SupplierAddress = sequelize.define(
'SupplierAddress',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
field: 'datacriacao',
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
field: 'dataalteracao',
},
address: {
type: DataTypes.STRING,
allowNull: false,
field: 'endereco',
},
supplierId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'idfornecedor',
},
},
{
underscored: true,
freezeTableName: true,
timestamps: false,
tableName: 'enderecofornecedor',
},
);
我期望可以通过以下方式获得结果:
SELECT * FROM supplier AS s INNER JOIN supplierAddress AS sa ON s.id = sa.supplierId;
如何使用急切加载来进行此咨询?我是 Sequelize 的新手。
我尝试过类似的方法,但不起作用:
static async getSuppliers() {
try {
database.Supplier.hasMany(database.SupplierAddress, {
foreignKey: 'supplierId'
});
database.SupplierAddress.belongsTo(database.Supplier);
const result = await database.Supplier.findAll({ include: database.SupplierAddress});
console.log(JSON.stringify(result, null, 2));
const list = await Promise.all(result);
return list;
} catch (error) {
error.location =
' Exception raised by getSuppliersin SupplierService. ';
throw error;
}
}
最佳答案
const supplierModels = await database.Supplier.findAll({
include: [{
model: database.SupplierAddress,
// this option is for INNER JOIN
required: true
}
});
const plainSupplierObjects = supplierModels.map(x => x.get({ plain: true })
console.log(JSON.stringify(plainSupplierObjects, null, 2));
// you don't need this line because you already got all Supplier records asynchronously.
//const list = await Promise.all(result);
return plainSupplierObjects;
此外,您不需要每次在获得结果之前都定义关联。只需在 Sequelize 中注册模型后立即执行此操作一次。
关于node.js - 对没有关系的关系数据库使用急切加载sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60419010/
我有一个案例,在大多数情况下,对象之间的关系是这样的,因此在关系上预先配置一个渴望(加入)的负载是有意义的。但是现在我遇到了一种情况,我真的不想完成急切的加载。 我是否应该从关系中删除连接负载并将所有
在我的 Grails 项目中,我有以下类: class A { static hasMany = [cs:C] } class B { static hasMany = [cs:C]
想象一下以下简化的 DI 模型: @ApplicationScoped public class A { private B b; @Inject public A(B b)
我使用 MapLoader 将数据从数据存储初始加载到 Hazelcast (InitialLoadMode = EAGER)。我需要从一个物化 View 中加载这些数据,该 View 是为了在加载过
我使用 Hibernate Envers 4.3.10.Final。我有以下两个 JPA 类: public class Factory { private int factoryID;
EJB 似乎被延迟加载 - 每当访问时。 但是,我想急切地初始化它们 - 即每当容器启动时。这是如何实现的(尤其是在 JBoss 中) This topic给出了一些提示,但不是很令人满意。 最佳答案
我是一名优秀的程序员,十分优秀!