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

Schemas with the same table name can not be synced with constraints #251

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
75 changes: 62 additions & 13 deletions src/sscce-sequelize-6.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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);
}