diff --git a/lib/dialects/postgres/data-types.js b/lib/dialects/postgres/data-types.js index 39c0a75d0fa4..3e5c6b447a2b 100644 --- a/lib/dialects/postgres/data-types.js +++ b/lib/dialects/postgres/data-types.js @@ -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; diff --git a/test/integration/dialects/postgres/dao.test.js b/test/integration/dialects/postgres/dao.test.js index c47e39629dee..1edd99d1af37 100644 --- a/test/integration/dialects/postgres/dao.test.js +++ b/test/integration/dialects/postgres/dao.test.js @@ -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,