diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index c90761b96..583851f7d 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -1,10 +1,10 @@ -import { DataTypes, Model } from 'sequelize'; +import { DataTypes } from 'sequelize'; import { createSequelize6Instance } from '../setup/create-sequelize-instance'; import { expect } from 'chai'; import sinon from 'sinon'; // 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']); +export const testingOnDialects = new Set(['postgres']); // You can delete this file if you don't want your SSCCE to be tested against Sequelize 6 @@ -19,23 +19,72 @@ export async function run() { // For less clutter in the SSCCE timestamps: false, }, - }); + }) - class Foo extends Model {} + // Create schemas + await sequelize.createSchema('schema_a', {}) + await sequelize.createSchema('schema_b', {}) - Foo.init({ - name: DataTypes.TEXT, - }, { - sequelize, - modelName: 'Foo', - }); + // Table `users` within schema `schema_a` + const User1 = sequelize.define( + 'User', + { + id: { + type: DataTypes.BIGINT, + allowNull: false, + primaryKey: true, + autoIncrement: true, + }, + }, + { + tableName: 'users', + schema: 'schema_a' + } + ) + + // Create `files` within schema `schema_a` that belongs to `user` + const File = sequelize.define( + 'File', + { + id: { + type: DataTypes.BIGINT, + allowNull: false, + primaryKey: true, + autoIncrement: true, + }, + }, + { + tableName: 'files', + schema: 'schema_a' + } + ) + + File.belongsTo(User1, { + as: 'file', + foreignKey: 'fileId', + constraints: true, // Set this to false and use the `User2` model and it will work just right. + }) + + // Table `user` within schema `schema_b` + const User2 = sequelize.define( // Comment this model and use constraints with `belongsTo` and it will work just right. + 'User', + { + id: { + type: DataTypes.BIGINT, + allowNull: false, + primaryKey: true, + autoIncrement: true, + }, + }, + { + tableName: 'users', + schema: 'schema_b' + } + ) // 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; - - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); }