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

Renaming attributes with eager loading returns error #1556

Closed
OscarHYCheung opened this issue Mar 26, 2014 · 8 comments
Closed

Renaming attributes with eager loading returns error #1556

OscarHYCheung opened this issue Mar 26, 2014 · 8 comments
Labels
type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@OscarHYCheung
Copy link

Users
    .findAll({
        include: [{
            model: Profiles,
            attributes: [
                'id',
                'avatar',
                ['first_name', 'firstName']
            ]
        }]
    })
    .success(function (users) {
        result.data = users;
        result.total = users.length;
        res.json(result);
    });

This gives error:

Executing (default): SELECT * FROM `users` WHERE `users`.`id`=1 AND `users`.`deleted_at` IS NULL LIMIT 1;
TypeError: Object first_name,firstName has no method 'replace'
    at Object.module.exports.removeTicks (/Users/project/node_modules/sequelize/lib/utils.js:528:14)
    at Object.module.exports.addTicks (/Users/project/node_modules/sequelize/lib/utils.js:524:29)
    at Object.module.exports.QueryGenerator.quoteIdentifier (/Users/project/node_modules/sequelize/lib/dialects/mysql/query-generator.js:412:20)
    at /Users/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:614:60
    at Array.map (native)
    at generateJoinQueries (/Users/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:613:46)
    at Object.<anonymous> (/Users/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:743:29)
    at Array.forEach (native)
    at Object.module.exports.QueryGenerator.selectQuery (/Users/project/node_modules/sequelize/lib/dialects/abstract/query-generator.js:742:25)
    at module.exports.QueryInterface.select (/Users/project/node_modules/sequelize/lib/query-interface.js:714:35)
@mickhansen
Copy link
Contributor

Aliasing attributes for includes is actually probably not supported at this time, i don't think there's any tests for it or anything like that.

@ghost
Copy link

ghost commented Apr 24, 2014

My solution is adding a getter method to the model :

    getterMethods : {
        firstName : function() {
            return this.getDataValue('first_name');
        },
  }

@mickhansen
Copy link
Contributor

Fixed by #1916

@nomadster
Copy link

What is the version that include this PR? I'm on 1.7.10 and get

Object [object Array] has no method 'replace'

when doing

Company.find({
                where: {company_id: company_id},
                attributes: [
                    ['company_max_allowed_user', 'maxUsers']
                ],
                include: [{
                    model: License, as: 'Licenses',
                    attributes: [
                        ['license_type', 'type'],
                        ['license_duration', 'duration'],
                        ['license_autorenew', 'autorenew']
                    ],
                    include: [{
                        model: Product, as: 'Products',
                        attributes: [
                            ['product_title','title'],
                            ['product_image','image']
                        ]
                    }]
                }]

More useful infos:

TypeError: Object [object Array] has no method 'replace'
    at Object.module.exports.removeTicks (/node_modules/sequelize/lib/utils.js:532:14)
    at Object.module.exports.addTicks (/node_modules/sequelize/lib/utils.js:528:29)
    at Object.module.exports.QueryGenerator.quoteIdentifier (/node_modules/sequelize/lib/dialects/mysql/query-generator.js:414:20)
    at /node_modules/sequelize/lib/dialects/abstract/query-generator.js:614:60
    at Array.map (native)
    at generateJoinQueries (/node_modules/sequelize/lib/dialects/abstract/query-generator.js:613:46)
    at Object.<anonymous> (/node_modules/sequelize/lib/dialects/abstract/query-generator.js:743:29)
    at Array.forEach (native)
    at Object.module.exports.QueryGenerator.selectQuery (/node_modules/sequelize/lib/dialects/abstract/query-generator.js:742:25)
    at module.exports.QueryInterface.select (/node_modules/sequelize/lib/query-interface.js:721:35)

@mickhansen
Copy link
Contributor

You can check what tags the commit have, have a look at: 6541fb4

The PR mentioned is only on 2.0.0-dev12 and up.

@milovanderlinden
Copy link

It works in almost any case, except when using limit and offset. I found this out today when trying to implement paging. Will dig in the issue deeper to see if I can find out where the alias is used to join instead of the real field name.

@milovanderlinden
Copy link

It happens with a findAll that has included models

  var options = {
    attributes: [
      ['id', 'service_request_id'],
      ['category_id', 'service_code'],
      'status',
      ['location', 'address'],
      ['latitude', 'lat'],
      ['longitude', 'long'],
      ['enteredDate', 'requested_datetime'],
      ['lastModified', 'updated_datetime']
    ],
    include: [{
      model: models.service,
      attributes: ['service_name']
    }, {
      model: models.issue,
      attributes: ['description'],
      include: [{
        model: models.media
      }]
    }, {
      model: models.person,
      attributes: ['firstname', 'middlename', 'lastname'],
      include: [{
        model: models.department,
        attributes: ['name']
      }]
    }],
    order: [
      ['enteredDate', 'DESC']
    ],
    offset: 0,
    limit: 10

  };


models.request.findAll(options).then(function(results) {});

@felixfbecker
Copy link
Contributor

@milovanderlinden please open a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

5 participants