Skip to content
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

implement "Inconsistent through association name" SSCCE #275

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions src/sscce-sequelize-6.ts

This file was deleted.

56 changes: 40 additions & 16 deletions src/sscce-sequelize-7.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { BelongsToMany } from '@sequelize/core/decorators-legacy';
import { BelongsToManyAddAssociationMixin, DataTypes, Model } from '@sequelize/core';
import { createSequelize7Instance } from '../dev/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
import { BelongsToManyGetAssociationsMixin } from 'sequelize';

// if your issue is dialect specific, remove the dialects you don't need to test on.
export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb', 'postgres', 'postgres-native']);
Expand All @@ -11,6 +12,18 @@ export const testingOnDialects = new Set(['mssql', 'sqlite', 'mysql', 'mariadb',

// Your SSCCE goes inside this function.
export async function run() {
class From extends Model {
@BelongsToMany(() => To, { through: () => Through})
declare to?: To[];

declare addTo: BelongsToManyAddAssociationMixin<To, number>;
declare getTo: BelongsToManyGetAssociationsMixin<To>
}

class To extends Model {}

class Through extends Model {}

// This function should be used instead of `new Sequelize()`.
// It applies the config for your SSCCE to work on CI.
const sequelize = createSequelize7Instance({
Expand All @@ -20,24 +33,35 @@ export async function run() {
// For less clutter in the SSCCE
timestamps: false,
},
models: [From, To, Through]
});

class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
declare id: CreationOptional<number>;
await sequelize.sync();

@Attribute(DataTypes.TEXT)
@NotNull
declare name: string;
}
const [from, to] = await Promise.all([
From.create(),
To.create()
]);

await from.addTo(to);

const eagerLoadedTo = (await From.findOne({
include: [To],
rejectOnEmpty: true
})).to!;

const lazyLoadedTo = await (await From.findOne({
rejectOnEmpty: true
})).getTo();

sequelize.addModels([Foo]);
console.log(eagerLoadedTo);
console.dir(lazyLoadedTo);

// 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;
// The through model is loaded as the model name for eager-loaded associations
expect('Through' in eagerLoadedTo);
expect(!('fromTo' in eagerLoadedTo));

console.log(await Foo.create({ name: 'TS foo' }));
expect(await Foo.count()).to.equal(1);
// But as the name of the association
expect(!('Through' in lazyLoadedTo));
expect('fromTo' in lazyLoadedTo);
}