From 1882f3cd9c42c245d486950b3a9cb18b761e1536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=A9?= Date: Tue, 25 Jan 2022 11:43:42 +0100 Subject: [PATCH] fix: rollback PR #13951 in v6 (#14004) --- lib/model.js | 14 +++++----- test/unit/model/find-by-pk.test.js | 42 ------------------------------ test/unit/model/find-one.test.js | 34 ------------------------ 3 files changed, 8 insertions(+), 82 deletions(-) delete mode 100644 test/unit/model/find-by-pk.test.js diff --git a/lib/model.js b/lib/model.js index e95d940a382d..971929411849 100644 --- a/lib/model.js +++ b/lib/model.js @@ -314,7 +314,7 @@ class Model { /** * Returns the attributes of the model. - * + * * @returns {object|any} */ static getAttributes() { @@ -1916,7 +1916,8 @@ class Model { } // Bypass a possible overloaded findOne - return await Model.findOne.call(this, options); + // note: in v6, we don't bypass overload https://github.com/sequelize/sequelize/issues/14003 + return await this.findOne(options); } /** @@ -1950,9 +1951,10 @@ class Model { } // Bypass a possible overloaded findAll. - return await Model.findAll.call(this, (_.defaults(options, { - plain: true, - }))); + // note: in v6, we don't bypass overload https://github.com/sequelize/sequelize/issues/14003 + return await this.findAll(_.defaults(options, { + plain: true + })); } /** @@ -2569,7 +2571,7 @@ class Model { * @param {boolean} [options.hooks=true] Run before / after bulk create hooks? * @param {boolean} [options.individualHooks=false] Run before / after create hooks for each individual Instance? BulkCreate hooks will still be run if options.hooks is true. * @param {boolean} [options.ignoreDuplicates=false] Ignore duplicate values for primary keys? (not supported by MSSQL or Postgres < 9.5) - * @param {Array} [options.updateOnDuplicate] Fields to update if row key already exists (on duplicate key update)? (only supported by MySQL, MariaDB, SQLite >= 3.24.0 & Postgres >= 9.5). + * @param {Array} [options.updateOnDuplicate] Fields to update if row key already exists (on duplicate key update)? (only supported by MySQL, MariaDB, SQLite >= 3.24.0 & Postgres >= 9.5). * @param {Transaction} [options.transaction] Transaction to run query under * @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql. * @param {boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). diff --git a/test/unit/model/find-by-pk.test.js b/test/unit/model/find-by-pk.test.js deleted file mode 100644 index d87dfa0399e7..000000000000 --- a/test/unit/model/find-by-pk.test.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict'; - -const chai = require('chai'); - -const expect = chai.expect; -const Support = require('../support'); - -const Sequelize = Support.Sequelize; -const Op = Sequelize.Op; -const current = Support.sequelize; -const sinon = require('sinon'); -const DataTypes = require('../../../lib/data-types'); - -describe(Support.getTestDialectTeaser('Model'), () => { - describe('method findByPk', () => { - beforeEach(function () { - this.stub = sinon.stub(Sequelize.Model, 'findAll').resolves(); - }); - afterEach(() => { - sinon.restore(); - }); - - it('should call internal findOne() method if findOne() is overridden', async () => { - const Model = current.define('model', { - unique1: { - type: DataTypes.INTEGER, - unique: 'unique', - }, - unique2: { - type: DataTypes.INTEGER, - unique: 'unique', - }, - }); - Model.findOne = sinon.stub(); - sinon.spy(Sequelize.Model, 'findOne'); - - await Model.findByPk(1); - Model.findOne.should.not.have.been.called; - Sequelize.Model.findOne.should.have.been.called; - }); - }); -}); diff --git a/test/unit/model/find-one.test.js b/test/unit/model/find-one.test.js index 01f8d0f25198..a4ce8358cb94 100644 --- a/test/unit/model/find-one.test.js +++ b/test/unit/model/find-one.test.js @@ -105,39 +105,5 @@ describe(Support.getTestDialectTeaser('Model'), () => { await Model.findOne({ where: { unique1: 42 } }); expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit'); }); - it('should call internal findAll() method if findOne() is overridden', async () => { - const Model = current.define('model', { - unique1: { - type: DataTypes.INTEGER, - unique: 'unique', - }, - unique2: { - type: DataTypes.INTEGER, - unique: 'unique', - }, - }); - Model.findAll = sinon.stub(); - - await Model.findOne(); - Model.findAll.should.not.have.been.called; - Sequelize.Model.findAll.should.have.been.called; - }); - it('should call internal findAll() method if findOne() is overridden', async () => { - const Model = current.define('model', { - unique1: { - type: DataTypes.INTEGER, - unique: 'unique', - }, - unique2: { - type: DataTypes.INTEGER, - unique: 'unique', - }, - }); - Model.findAll = sinon.stub(); - - await Model.findOne(); - Model.findAll.should.not.have.been.called; - Sequelize.Model.findAll.should.have.been.called; - }); }); });