From 7ff4c945b5fbec0815b33aa80cc7e0b601b65c1a Mon Sep 17 00:00:00 2001 From: dspintelegence <124267844+dspintelegence@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:01:31 +0100 Subject: [PATCH 1/4] Schemas with the same table name can not be synced if constraints have been created --- src/sscce-sequelize-6.ts | 75 +++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index c90761b96..d20fcc601 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 `schemaA` + const User1 = sequelize.define( + 'User', + { + id: { + type: DataTypes.BIGINT, + allowNull: false, + primaryKey: true, + autoIncrement: true, + }, + }, + { + tableName: 'users', + schema: 'schema_a' + } + ) + + // Create `files` within schema `schemaA` 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 run work right. + }) + + // Table `user` within schema `schemaB` + 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: 'users2', + 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); } From 49570ee16bd9205a580f45e1001f1a2f4693aa89 Mon Sep 17 00:00:00 2001 From: dspintelegence <124267844+dspintelegence@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:11:57 +0100 Subject: [PATCH 2/4] docs: adjusted comments --- src/sscce-sequelize-6.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index d20fcc601..ecf4b8b17 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -25,7 +25,7 @@ export async function run() { await sequelize.createSchema('schema_a', {}) await sequelize.createSchema('schema_b', {}) - // Table `users` within schema `schemaA` + // Table `users` within schema `schema_a` const User1 = sequelize.define( 'User', { @@ -42,7 +42,7 @@ export async function run() { } ) - // Create `files` within schema `schemaA` that belongs to `user` + // Create `files` within schema `schema_a` that belongs to `user` const File = sequelize.define( 'File', { @@ -65,7 +65,7 @@ export async function run() { constraints: true, // Set this to false and use the `User2` model and it will run work right. }) - // Table `user` within schema `schemaB` + // 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', { From b96b44ba192601f50755b5823777fca2d0c6ae72 Mon Sep 17 00:00:00 2001 From: dspintelegence <124267844+dspintelegence@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:12:59 +0100 Subject: [PATCH 3/4] docs: typo in explanation --- src/sscce-sequelize-6.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index ecf4b8b17..567cdefcb 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -62,7 +62,7 @@ export async function run() { File.belongsTo(User1, { as: 'file', foreignKey: 'fileId', - constraints: true, // Set this to false and use the `User2` model and it will run work right. + constraints: true, // Set this to false and use the `User2` model and it will work just right. }) // Table `user` within schema `schema_b` From b3dd40e1dd3c9d49f8ff1a6a728543c4f2db06a6 Mon Sep 17 00:00:00 2001 From: dspintelegence <124267844+dspintelegence@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:13:55 +0100 Subject: [PATCH 4/4] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index 567cdefcb..583851f7d 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -77,7 +77,7 @@ export async function run() { }, }, { - tableName: 'users2', + tableName: 'users', schema: 'schema_b' } )