Skip to content

Commit

Permalink
feat: option for attributes having dotNotation (#13670)
Browse files Browse the repository at this point in the history
* feat: option for attributes having dotnotation

- `options.dotnotation`, can be used when the column name has dot in it.

* test: add test case for attributes with dot notation

* docs: add function doc for `option.dotnotation`

* fix: expected query for dot notation test case

* refactor: camelcase dotnotation keyword

Co-authored-by: Mukesh Suthar <mukesh@stepsetgo.com>
  • Loading branch information
SutharMukesh and Mukesh Suthar committed Nov 18, 2021
1 parent 9591573 commit 41876f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dialects/abstract/query-generator.js
Expand Up @@ -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}`;
}

Expand Down
1 change: 1 addition & 0 deletions lib/model.js
Expand Up @@ -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}
Expand Down
36 changes: 36 additions & 0 deletions test/unit/sql/select.test.js
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 41876f1

Please sign in to comment.