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

Previous value for VIRTUAL data returns undefined #11640

Open
5 tasks
mschipperheyn opened this issue Nov 3, 2019 · 4 comments
Open
5 tasks

Previous value for VIRTUAL data returns undefined #11640

mschipperheyn opened this issue Nov 3, 2019 · 4 comments
Labels
existing workaround For issues. There is a known workaround for this issue. status: understood For issues. Applied when the issue is understood / reproducible. topic: virtual fields For issues and PRs. Things that involve virtual fields. type: bug

Comments

@mschipperheyn
Copy link

Issue Description

model

{
	id: {
		type: DataTypes.UUID,
		defaultValue: DataTypes.UUIDV4,
		primaryKey: true,
		lowercase: true,
	},
	firstName: {
		type: DataTypes.STRING(255),
	},
	lastName: {
		type: DataTypes.STRING(255),
	},
	displayName: {
		type: DataTypes.VIRTUAL(DataTypes.STRING, [
			'firstName',
			'lastName',
		]),
		get() {
			return `${this.get('firstName') || ''} ${this.get('lastName') ||
				''}`.trim();
		},
	},
},
{
	sequelize,
	hooks: {
		afterUpdate: (item, options) => {
			console.log(item._previousDataValues.displayName); // undefined
			console.log(item.previous('displayName')); // undefined

			console.log(item._previousDataValues.firstName); // value
			console.log(item.previous('firstName')); // value
			
			
		}
	},
},

What are you doing?

updating a record

What do you expect to happen?

I expect VIRTUAL fields to be populated with the actual data

What is actually happening?

previous values are undefined

Additional context

There's obviously a workaround for this, so, I'm registering this also for other people to be aware.

Environment

  • Sequelize version: 4.44.3
  • Node.js version: 12.13.0
  • Operating System: Mac os

Issue Template Checklist

How does this problem relate to dialects?

  • [x ] I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and
    database version Mysql 5.7

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • [x ] No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@papb papb added status: understood For issues. Applied when the issue is understood / reproducible. type: bug existing workaround For issues. There is a known workaround for this issue. topic: virtual fields For issues and PRs. Things that involve virtual fields. labels Nov 3, 2019
@coolhand79
Copy link

I'm interested to know what the known workaround for this is... simply not to use them?

@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 5, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@blapaz
Copy link

blapaz commented Apr 12, 2022

This still appears to be an issue in v6

@ephys
Copy link
Member

ephys commented Apr 12, 2022

VIRTUAL types are basically just getters & setters, they don't have an entry in dataValues or previousDataValues

Which makes me wonder if we should not deprecate VIRTUAL altogether, and recommend using native getter & setters instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
existing workaround For issues. There is a known workaround for this issue. status: understood For issues. Applied when the issue is understood / reproducible. topic: virtual fields For issues and PRs. Things that involve virtual fields. type: bug
Projects
None yet
Development

No branches or pull requests

6 participants