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

Index not created when table already exists in diffrent schema (Postgres) #15543 #244

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"type": "commonjs",
"license": "MIT",
"dependencies": {
"@sequelize/core": "^7.0.0-alpha.10",
"chai": "^4",
"chai-as-promised": "^7",
"chai-datetime": "^1",
"chalk": "^4.1.2",
"cross-env": "^7",
"fs-jetpack": "^4",
"pg": "^8.8.0",
"sequelize": "^6",
"@sequelize/core": "^7.0.0-alpha.10",
"sinon": "^13",
"sinon-chai": "^3"
},
Expand Down
113 changes: 90 additions & 23 deletions src/sscce-sequelize-6.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,108 @@
import { DataTypes, Model } from 'sequelize';
import { createSequelize6Instance } from '../setup/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
import {DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize} from 'sequelize';
import {createSequelize6Instance} from '../setup/create-sequelize-instance';
import assert from "assert";

// 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
class TestModel extends Model<InferAttributes<TestModel>, InferCreationAttributes<TestModel>> {
declare readonly id: string;
declare readonly name: string;
}

function initTestModel(sequelize: Sequelize) {
TestModel.init(
{
id: {type: DataTypes.UUID, primaryKey: true},
name: {type: DataTypes.UUID, allowNull: false}
},
{
sequelize: sequelize,
timestamps: false,
modelName: 'example',
indexes: [
{
type: 'UNIQUE',
fields: ['id', 'name']
}
]
}
);
}

// Your SSCCE goes inside this function.
export async function run() {
// This function should be used instead of `new Sequelize()`.
// It applies the config for your SSCCE to work on CI.
// Delete and then recreate schema so we know they are empty.
const sequelize = createSequelize6Instance({
logQueryParameters: true,
benchmark: true,
});
await sequelize.query('DROP SCHEMA IF EXISTS public, test CASCADE');
await sequelize.query('CREATE SCHEMA public');
await sequelize.query('CREATE SCHEMA test');

// Instance 1, table will be created as excepted.
const sequelize1 = createSequelize6Instance({
logQueryParameters: true,
benchmark: true,
schema: 'test',
define: {
// For less clutter in the SSCCE
timestamps: false,
},
schema: 'test'
}
});
initTestModel(sequelize1);
await sequelize1.sync({force: true});

class Foo extends Model {}
// Instance 2, table will be created, but index is not created.
const sequelize2 = createSequelize6Instance({
logQueryParameters: true,
benchmark: true,
schema: undefined, // eg schema "public"
define: {
schema: undefined, // eg schema "public"
}
});
initTestModel(sequelize2);
await sequelize2.sync({force: true})

Foo.init({
name: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Foo',
// Verify the results.
const index = await sequelize2.getQueryInterface().showIndex({
tableName: 'examples',
schema: 'public',
delimiter: '.'
});

// 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;
// Current workaround for the issue is in the "sequelize2" instead of "undefined"
// use the postgres default schema value "public", this will work as excepted.
// the issue from what I can see is when sequelize try to create the table in the sequelize2
// the system detect an index with the same name already exists (eg the index belong to the "test" schema)
// and then do not create the index, even if we're talking about two different table in two different schemas.

assert.deepStrictEqual(index,
[
{
name: 'examples_id_name',
primary: false,
unique: true,
indkey: '1 2',
definition: 'CREATE UNIQUE INDEX examples_id_name ON public.examples USING btree (id, name)',
fields: [
{attribute: 'id', collate: undefined, length: undefined, order: undefined},
{attribute: 'name', collate: undefined, length: undefined, order: undefined}
]
},
{
name: 'examples_pkey',
primary: true,
unique: true,
indkey: '1',
definition: 'CREATE UNIQUE INDEX examples_pkey ON public.examples USING btree (id)',
fields: [
{attribute: 'id', collate: undefined, length: undefined, order: undefined}
]
}
]);


console.log(await Foo.create({ name: 'TS foo' }));
expect(await Foo.count()).to.equal(1);
}
41 changes: 0 additions & 41 deletions src/sscce-sequelize-7.ts

This file was deleted.