简体   繁体   English

Sequelize :适用于 where 条件不适用于单数和复数的范围

[英]Sequelize : Scope with where condition not working with singular and plural

I am looking to put a valid condition in the child running for a 1: 1 and N: N relationship like this example 1:1我希望在运行 1:1 和 N:N 关系的孩子中设置一个有效条件,就像这个例子 1:1

// Find all projects with a least one task where task.state === project.state
Project.findAll({
    include: [{
        model: Task,
        where: { state: Sequelize.col('project.state') }
    }]
})

Only the name of the table changes name from "project" to "projects" depending on the relationship In this case, 'project.state' will work but a request from a model with an N: N association will give an error :只有表的名称根据关系将名称从“project”更改为“projects”在这种情况下,“project.state”将起作用,但来自具有 N:N 关联的模型的请求将给出错误:

"invalid reference to FROM-clause entry for table \" project \ ""

the condition should be 'projects.state'条件应该是“projects.state”

If working if i use "as :project" in include for each query but if anyone has a good idea.如果我在每个查询的 include 中使用“as :project”,但如果有人有好主意,则可以工作。

Thanks for your help谢谢你的帮助

The best way is to force sequelize.js to use the original names mentioned in model while creating tables in database you can make sequelize.js to use given names not plural of names最好的方法是强制 sequelize.js 使用模型中提到的原始名称,同时在数据库中创建表您可以使 sequelize.js 使用给定的名称而不是复数名称

var db_instance = new Sequelize(config.DB.database, config.DB.username, config.DB.password, {
  host: config.DB.host,
  dialect: config.DB.dialect,
  define: {
    timestamps: true,
    freezeTableName: true
  },
  logging: false
});  

OR或者

You can simply tell Sequelize the name of the table directly as well:您也可以直接告诉 Sequelize 表的名称:

sequelize.define('project', {
  // ... (attributes)
}, {
  tableName: 'project'
});

You can see both Method in Documentation of sequelize.js您可以在 sequelize.js 的文档中看到这两种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM