Jul
Jul

Reputation: 1

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

// 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 :

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

the condition should be 'projects.state'

If working if i use "as :project" in include for each query but if anyone has a good idea.

Thanks for your help

Upvotes: 0

Views: 603

Answers (1)

Aryan
Aryan

Reputation: 3638

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

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.define('project', {
  // ... (attributes)
}, {
  tableName: 'project'
});

You can see both Method in Documentation of sequelize.js

Upvotes: 0

Related Questions