From 3aae67db8493405d761542ea2dd50ecb77c4ae36 Mon Sep 17 00:00:00 2001 From: Nick Rabinowitz Date: Tue, 25 Apr 2023 11:11:31 -0700 Subject: [PATCH] Add repro case for scoped findAndCountAll failure --- src/sscce-sequelize-6.ts | 47 ++++++++++++++++++++++++++++------------ src/sscce-sequelize-7.ts | 47 ++++++++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 28 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index c90761b96..a612f823d 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -21,21 +21,40 @@ export async function run() { }, }); - class Foo extends Model {} + class FooModel extends Model {} - Foo.init({ - name: DataTypes.TEXT, - }, { - sequelize, - modelName: 'Foo', - }); + FooModel.init( + { + id: {type: DataTypes.STRING, primaryKey: true} + }, + {sequelize, modelName: 'FooModel'} + ); + + await FooModel.sync(); - // You can use sinon and chai assertions directly in your SSCCE. - const spy = sinon.spy(); - sequelize.afterBulkSync(() => spy()); - await sequelize.sync({ force: true }); - expect(spy).to.have.been.called; + class OtherModel extends Model {} + + OtherModel.init( + { + id: {type: DataTypes.STRING, primaryKey: true}, + modelId: {type: DataTypes.STRING, field: 'model_id'}, + modelType: {type: DataTypes.ENUM('foo', 'bar'), field: 'model_type'} + }, + {sequelize, modelName: 'OtherModel'} + ); - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); + await OtherModel.sync(); + + FooModel.hasMany(OtherModel, { + foreignKey: 'modelId', + scope: { + modelType: 'foo' + } + }); + + await FooModel.findAndCountAll({ + include: { + model: OtherModel + } + }); } diff --git a/src/sscce-sequelize-7.ts b/src/sscce-sequelize-7.ts index 861b9fdea..05a5334a5 100644 --- a/src/sscce-sequelize-7.ts +++ b/src/sscce-sequelize-7.ts @@ -21,21 +21,40 @@ export async function run() { }, }); - class Foo extends Model {} + class FooModel extends Model {} - Foo.init({ - name: DataTypes.TEXT, - }, { - sequelize, - modelName: 'Foo', - }); + FooModel.init( + { + id: {type: DataTypes.STRING, primaryKey: true} + }, + {sequelize, modelName: 'FooModel'} + ); + + await FooModel.sync(); - // You can use sinon and chai assertions directly in your SSCCE. - const spy = sinon.spy(); - sequelize.afterBulkSync(() => spy()); - await sequelize.sync({ force: true }); - expect(spy).to.have.been.called; + class OtherModel extends Model {} + + OtherModel.init( + { + id: {type: DataTypes.STRING, primaryKey: true}, + modelId: {type: DataTypes.STRING, field: 'model_id'}, + modelType: {type: DataTypes.ENUM('foo', 'bar'), field: 'model_type'} + }, + {sequelize, modelName: 'OtherModel'} + ); - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); + await OtherModel.sync(); + + FooModel.hasMany(OtherModel, { + foreignKey: 'modelId', + scope: { + modelType: 'foo' + } + }); + + await FooModel.findAndCountAll({ + include: { + model: OtherModel + } + }); }