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

fix(13804): use schema for ARRAY(ENUM) type name #13807

Merged
merged 3 commits into from Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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