Skip to content

Commit

Permalink
fix(postgres): allows usage of schema for ARRAY(ENUM) type name (#13807)
Browse files Browse the repository at this point in the history
* fix(13804): use schema for ARRAY(ENUM) type name

* fix(31804): Update integration test

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
nahog and WikiRik committed Dec 22, 2021
1 parent 49fb726 commit da5b0ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/dialects/postgres/data-types.js
Expand Up @@ -479,13 +479,19 @@ module.exports = BaseTypes => {
let castKey = this.toSql();

if (this.type instanceof BaseTypes.ENUM) {
const table = options.field.Model.getTableName();
const useSchema = table.schema !== undefined;
const schemaWithDelimiter = useSchema ? `${Utils.addTicks(table.schema, '"')}${table.delimiter}` : '';

castKey = `${Utils.addTicks(
Utils.generateEnumName(options.field.Model.getTableName(), options.field.field),
Utils.generateEnumName(useSchema ? table.tableName : table, options.field.field),
'"'
) }[]`;
}

str += `::${castKey}`;
str += `::${schemaWithDelimiter}${castKey}`;
} else {
str += `::${castKey}`;
}
}

return str;
Expand Down
32 changes: 32 additions & 0 deletions test/integration/dialects/postgres/dao.test.js
Expand Up @@ -465,6 +465,38 @@ if (dialect.match(/^postgres/)) {
expect(user.length).to.equal(1);
});

it('should be able to insert a new record even with an array of enums in a schema', async function() {
const schema = 'special_schema';
this.sequelize.createSchema(schema);
const User = this.sequelize.define('UserEnums', {
name: DataTypes.STRING,
type: DataTypes.ENUM('A', 'B', 'C'),
owners: DataTypes.ARRAY(DataTypes.STRING),
specialPermissions: {
type: DataTypes.ARRAY(DataTypes.ENUM([
'access',
'write',
'check',
'delete'
])),
field: 'special_permissions'
}
}, {
schema
});

await User.sync({ force: true });

const user = await User.bulkCreate([{
name: 'file.exe',
type: 'C',
owners: ['userA', 'userB'],
specialPermissions: ['access', 'write']
}]);

expect(user.length).to.equal(1);
});

it('should fail when trying to insert foreign element on ARRAY(ENUM)', async function() {
const User = this.sequelize.define('UserEnums', {
name: DataTypes.STRING,
Expand Down

0 comments on commit da5b0ce

Please sign in to comment.