question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Included models are failing to properly inject attributes for virtual fields

See original GitHub issue

What are you doing?

I have defined a model, Image, with the virtual field url. This virtual field is dependent on another attribute, path.

path: Sequelize.STRING,
url: {
	type: Sequelize.VIRTUAL(Sequelize.STRING, ['path']),
		get: function() {
		if (this.getDataValue('path')) {
			return `${process.env.IMAGE_ROOT}/${this.getDataValue('path')}`;
		} else {
			return null;
		}
	},
},

This model is associated with the User model. I have a User w/ id 1, and an Image belonging to that user (also w/ id 1). This image has a path, 'my-path'.

If I look up the image by id, everything works as expected:

// image.path === `${process.env.IMAGE_ROOT}/my-path`
const image = await Image.findByPk(1, { attributes: ['id', 'url'] })

However, if we look up the image by including on the user, image.path is null:

// user.image.path === null; should === `${process.env.IMAGE_ROOT}/my-path`
const user = await User.findByPk(1, { 
    attributes: ['id'], 
    include: { model: Image, attributes: ['id', 'url']} 
})

I can confirm Sequelize is generating the proper DB query, but isn’t assigning the path attribute on the retrieved image instance when it’s included, preventing us from generating the appropriate (virtual field) url in the getter.

I was able to solve this issue with the following modification, but am not familiar enough with the codebase to know if this is appropriate: https://github.com/redivis/sequelize/commit/d4ad97d33819ee1d6110909c761de98cd52be73f

Environment

Dialect:

  • postgres Database version: 11.x Sequelize version: 5.x Node Version: 12.x Tested with latest release:
  • No
  • Yes, specify that version:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
papbcommented, Aug 5, 2019

Note: I have started investigating this. Hopefully I will have time to complete it soon.

1reaction
imathewscommented, Nov 27, 2019

@papb @stevelacy @thieman https://github.com/sequelize/sequelize/pull/11713

I don’t have the bandwidth to update the tests, if anyone else wants to tackle…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Virtual attributes are ignored inside the include section #10552
attributes at all or start writing the query from the Video model* - the query works fine. The working query example: Video.findOne({ attributes...
Read more >
Virtual attributes with Devise in Rails 4 - ruby - Stack Overflow
My User model doesn't have a :name field but I want to include a :name field in my sign up form which will...
Read more >
Model - Sequelize
Initialize a model, representing a table in the DB, with attributes and options. The table columns are defined by the hash that is...
Read more >
Creating a More Complex Data Model for an ASP.NET MVC ...
To see an example of how to do that, you'll add an attribute to the ... order to ensure that the field renders...
Read more >
How to Test Rails Models with RSpec - Semaphore CI
Add the rspec-rails helper to the Gemfile : ... Failures: 1) Auction is valid with valid attributes Failure/Error: expect(Auction.new).to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found