sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.52k stars 4.27k forks source link

getAssociation failing during query #5474

Closed jamespedid closed 8 years ago

jamespedid commented 8 years ago

I'm having a bit of trouble with an association where the where clause that is being generated by the association is being rejected by the query builder:

AssertionError: expected promise to be fulfilled but it was rejected with [Error: where: "raw query" has been removed, please use where ["raw query", [replacements]]]

The calling query looks like this:

let ownerQuery = models.MapModel.then(map => {
    if (map == null) {
        throw new Error(`error`)
    }
    return map.getOwner().then(owner => {
        //this callback is not happening, failing with error noted above
        if (owner == null) {
            return Promise.resolve(models.Owners.build({mapModel: map.id}))
        }
        return Promise.resolve(owner);
    })
});

The models are set up like this:

models.Owner.MapModel = models.Owner.hasOne(models.MapModel, {
    foreignKey: 'id',
    as: 'map_models'
});

models.MapModel.Owner = models.MapModel.hasOne(models.Owner, {
    as: 'owner'
})

If you can find an error with this set up, please let me know. Otherwise, I can't seem to figure out why I'm getting this query problem.

janmeier commented 8 years ago

hasOne in both directions will add a foreign key to both MapModel and Owner - you probably want hasOne on one side and belongsTo on the other - http://docs.sequelizejs.com/en/latest/api/associations/

Not sure why its causing that error though - If you still experience the problem, could you create a SSCCE? (still missing model definitions and some test data in order to be able to test this )

jamespedid commented 8 years ago

I have identified the issue: I had modified the node package to log the selectQuery that was being executed, and it was calling the selectQuery function before the actual query. This was mutating the options object before intended, and was leading to that query error.

mohsensho commented 4 years ago

I have kind of the same issue. in my query, I have 14 conditions with and operator it's working until you involved more than few conditions. @jamespedid could you please share how did you fix it? thank you.