New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing raw query on tables not defined as models breaks Sequelize #4012

Closed
ivosabev opened this Issue Jun 25, 2015 · 4 comments

Comments

2 participants
@ivosabev

ivosabev commented Jun 25, 2015

An example will be inserting unique id twice. This will break because Sequielize will try to call something like "this.model.uniqueKeys", but this.model is undefined since no model exists.

Could this be changed so we can make raw queries without defining models.

@janmeier

This comment has been minimized.

Member

janmeier commented Jun 25, 2015

Which dialect and version are you on? As far as I remember all dialects should check for this.model before checking unique keys

@ivosabev

This comment has been minimized.

ivosabev commented Jun 25, 2015

I am using Postgresql, but the error is in the abstract/query.js

AbstractQuery.prototype.getUniqueConstraintErrorMessage = function(field) {
  var message = field + ' must be unique';
  var self = this;
  Object.keys(self.model.uniqueKeys).forEach(function(key) {
    if (self.model.uniqueKeys[key].fields.indexOf(field.replace(/"/g, '')) >= 0) {
      if (self.model.uniqueKeys[key].hasOwnProperty('msg')) {
        message = self.model.uniqueKeys[key].msg;
      }
    }
  });
  return message;
};

Here a stack

TypeError: Cannot read property 'uniqueKeys' of undefined
    at AbstractQuery.getUniqueConstraintErrorMessage (/home/hv/spotify-scraper/node_modules/sequelize/lib/dialects/abstract/query.js:420:25)
    at /home/hv/spotify-scraper/node_modules/sequelize/lib/dialects/postgres/query.js:379:18
    at /home/hv/spotify-scraper/node_modules/sequelize/node_modules/lodash/index.js:3106:15
    at baseForOwn (/home/hv/spotify-scraper/node_modules/sequelize/node_modules/lodash/index.js:2053:14)
    at Function.forOwn (/home/hv/spotify-scraper/node_modules/sequelize/node_modules/lodash/index.js:3388:16)
    at Query.formatError (/home/hv/spotify-scraper/node_modules/sequelize/lib/dialects/postgres/query.js:377:17)
    at null.<anonymous> (/home/hv/spotify-scraper/node_modules/sequelize/lib/dialects/postgres/query.js:104:19)
    at emit (events.js:95:17)
    at Query.handleError (/home/hv/spotify-scraper/node_modules/pg/lib/query.js:99:8)
    at null.<anonymous> (/home/hv/spotify-scraper/node_modules/pg/lib/client.js:166:26)
    at emit (events.js:95:17)
    at Socket.<anonymous> (/home/hv/spotify-scraper/node_modules/pg/lib/connection.js:109:12)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:427:10)

@janmeier janmeier added the bug label Jun 28, 2015

@janmeier

This comment has been minimized.

Member

janmeier commented Jun 28, 2015

Adding a check for if self.model before that should be fine - PR welcome, now that you've already identified the right spot :)

@janmeier

This comment has been minimized.

Member

janmeier commented Jun 29, 2015

Fixed it myself in connection with another issue - thanks for the report @ivosabev :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment