diff --git a/lib/dialects/abstract/query-generator.js b/lib/dialects/abstract/query-generator.js index 664487d95f87..5a438cd3268b 100644 --- a/lib/dialects/abstract/query-generator.js +++ b/lib/dialects/abstract/query-generator.js @@ -1452,7 +1452,7 @@ class QueryGenerator { ? this.quoteAttribute(attr, options.model) : this.escape(attr); } - if (!_.isEmpty(options.include) && !attr.includes('.') && addTable) { + if (!_.isEmpty(options.include) && (!attr.includes('.') || options.dotNotation) && addTable) { attr = `${mainTableAs}.${attr}`; } diff --git a/lib/model.js b/lib/model.js index 9a7dfcd386e0..9960422d2383 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1672,6 +1672,7 @@ class Model { * @param {object} [options.having] Having options * @param {string} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only) * @param {boolean|Error} [options.rejectOnEmpty=false] Throws an error when no records found + * @param {boolean} [options.dotNotation] Allows including tables having the same attribute/column names - which have a dot in them. * * @see * {@link Sequelize#query} diff --git a/test/unit/sql/select.test.js b/test/unit/sql/select.test.js index 6f44cd46bace..6aa33a5d8efe 100644 --- a/test/unit/sql/select.test.js +++ b/test/unit/sql/select.test.js @@ -826,6 +826,42 @@ describe(Support.getTestDialectTeaser('SQL'), () => { }); }); + it('attributes with dot notation', () => { + const User = Support.sequelize.define('User', { + name: DataTypes.STRING, + age: DataTypes.INTEGER, + 'status.label': DataTypes.STRING + }, + { + freezeTableName: true + }); + const Post = Support.sequelize.define('Post', { + title: DataTypes.STRING, + 'status.label': DataTypes.STRING + }, + { + freezeTableName: true + }); + + User.Posts = User.hasMany(Post, { foreignKey: 'user_id' }); + + expectsql(sql.selectQuery('User', { + attributes: ['name', 'age', 'status.label'], + include: Model._validateIncludedElements({ + include: [{ + attributes: ['title', 'status.label'], + association: User.Posts + }], + model: User + }).include, + model: User, + dotNotation: true + }, User), { + default: 'SELECT [User].[name], [User].[age], [User].[status.label], [Posts].[id] AS [Posts.id], [Posts].[title] AS [Posts.title], [Posts].[status.label] AS [Posts.status.label] FROM [User] AS [User] LEFT OUTER JOIN [Post] AS [Posts] ON [User].[id] = [Posts].[user_id];', + postgres: 'SELECT "User".name, "User".age, "User"."status.label", Posts.id AS "Posts.id", Posts.title AS "Posts.title", Posts."status.label" AS "Posts.status.label" FROM "User" AS "User" LEFT OUTER JOIN Post AS Posts ON "User".id = Posts.user_id;' + }); + }); + }); describe('raw query', () => {