Sequelize.js model.schema 与 include

标签 sequelize.js

我创建了一个名为 Person 的模型,它使用 .schema(tenant.name,{schemaDelimiter: '_'}) 将记录存储在单独的表中。
然后,为了获取相关记录,我使用以下语句:

Person.schema(tenant.name).findAll({where: ...});
这很好用!
现在我添加另一个名为 EventCase 的模型和以下关联:
Person.hasMany(EventCase,{as: 'events', foreignKey: 'entity_id', constraints: false, scope: {entity_type: 'IND'}});
我想使用带有包含选项的查找器。
基本上,我的代码是:
Person.schema(tenant.name).findAll({where: ...}, include: ['events']);
但我收到一个错误 (SequelizeDatabaseError","parent":{"code":"ER_NO_SUCH_TABLE","errno":1146,"sqlState":"42S02","sqlMessage":"Table 'data_eur1.events' 没有存在”),因为 Sequalize 生成查询的方式没有在事件表的名称前面加上模式。我期待 Sequelize 生成类似 'data_eur1.<%tenant.name%>_events'...
我可能以错误的方式使用了 sequelize,但我在文档或网络上都找不到任何相关内容。

最佳答案

我可能有一个解决方法:

Person.schema(tenant.name).findAll({where: ...}, include: [{
as:'events', model: EventCase.schema(tenant.name)]);
这似乎有效。但是,我并没有真正利用协会的附加值(value)。

关于Sequelize.js model.schema 与 include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66006392/

相关文章:

postgresql - 均衡 : comparing parent column and child column in where clause inside include

mysql - 如何与 Sequelize 建立多对多关联?

node.js - 如何在 Sequelize 中使用 between 运算符?

sequelize.js - 双方设置关联 Sequelize

node.js - 使用 nodejs sequelize pg.js "the dialect postgres is not supported"连接到 postgres 数据库时出错

sql - Sequelize 非主键上的连接表

orm - sequelize中allowNull和require的区别

sequelize.js - Sequelize 查询由 $and 连接的 2 个函数

node.js - 在 sequelize 5 中从 findOne 访问实例方法

sequelize.js - Sequelize js orm迁移与seeder