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

Updating a Model with custom validation bug #13432

Open
3 of 8 tasks
gsqrt2 opened this issue Aug 11, 2021 · 21 comments
Open
3 of 8 tasks

Updating a Model with custom validation bug #13432

gsqrt2 opened this issue Aug 11, 2021 · 21 comments
Assignees
Labels
breaking change For issues and PRs. Changes that break compatibility and require a major version increment. type: bug

Comments

@gsqrt2
Copy link

gsqrt2 commented Aug 11, 2021

Issue Creation Checklist

Bug Description

When trying to Model.update() a model with custom validation, an error is thrown as the custom validator does not have access to instance fields.

SSCCE

**Here is the link to the SSCCE for this issue: https://github.com/gsqrt2/sequelize-update-validation-error

What do you expect to happen?

In regard to the SSCCE:

Connection.update( {
        approved: true
    }, {
    where: {
        approved: false
    }
})

should update Connections where approved===false to approved: true

What is actually happening?

The custom validator throws, as instance dataValues are undefined

ValidationError [SequelizeValidationError]:  Validation error: Cannot connect to self.

Additional context

The SSCCE is extremely basic, just a dummy Connection with a custom validator that ensures that the two user ids are not equal. Commenting out the validation restores normal functionality.

Environment

  • Sequelize version: 6.6.5
  • Node.js version: v12.19.0

Bug Report Checklist

How does this problem relate to dialects?

  • 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 XXX

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.
  • 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.
@cathykc
Copy link

cathykc commented Aug 26, 2021

Also encountering this problem! Currently working around by using update on instance.

There's an old closed issue that seems related to this: #4447

@Kraicheck
Copy link

There's a related open issue as well: #12666

@github-actions
Copy link
Contributor

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 stale and removed stale labels Oct 27, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 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 4, 2021
@gsqrt2
Copy link
Author

gsqrt2 commented Nov 4, 2021

Still an issue

@github-actions github-actions bot removed the stale label Nov 5, 2021
@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. 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 20, 2021
@gsqrt2
Copy link
Author

gsqrt2 commented Nov 22, 2021

Still an issue

@github-actions github-actions bot removed the stale label Nov 23, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Dec 7, 2021

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

@github-actions github-actions bot added the stale label Dec 7, 2021
@gsqrt2
Copy link
Author

gsqrt2 commented Dec 7, 2021

If this is still an issue, just leave a comment or remove the "stale" label.

Is there a way I can remove the "stale" label myself instead of repeatedly commenting that it's still an issue?

@github-actions github-actions bot removed the stale label Dec 8, 2021
@github-actions
Copy link
Contributor

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

@github-actions github-actions bot added the stale label Dec 22, 2021
@WikiRik WikiRik added type: bug and removed stale labels Dec 22, 2021
@mypipsen
Copy link

Still relevant

@ephys
Copy link
Member

ephys commented Jan 22, 2022

This doesn't sound like a bug. Custom validators will be called every time a model will be updated - including when calling bulk updates.

Simply check whether the values your validator depend on are present before using them.

cannotConnectToSelf() {
  if (this.user1id === undefined || this.user2id === undefined) {
    return;
  }

  if (this.user1id === this.user2id) {
    throw new Error('Cannot connect to self.');
  }
},

You'd have the same issue if you selected a subset of your model's attributes in find then called save on the returned instance.

@ephys ephys closed this as completed Jan 22, 2022
@ephys ephys self-assigned this Jan 22, 2022
@ephys ephys removed the type: bug label Jan 22, 2022
@Kraicheck
Copy link

@ephys, I don't understand how you can suggest simply not validating because what you're trying to validate isn't available, not because the values don't exist, but because Sequelize made an internal choice not to retrieve them?
What use are validators that don't always validate or come to wrong conclusions because of incomplete knowledge?

@ephys
Copy link
Member

ephys commented Jan 24, 2022

Because that would completely destroy your application's performance. It would require running a select during your bulk update, which could select a very large amount of data.

If your constraint validation depends on more than one field, using check constraints would be much safer and efficient.

If you really want to select data before updating it (which is going to have a large performance impact), you can also use the individualHooks option.

@Kraicheck
Copy link

individualHooks indeed seems like it would do the trick. Thanks!
I know this will impact performance but that's not really an issue in my application.

@Kraicheck
Copy link

@ephys, individualHooks actually doesn't have an impact on this.
I've tried and verified in the source code; validation happens on the generic version with the default values regardless of the value for indivualHooks.

@ephys
Copy link
Member

ephys commented Jan 31, 2022

I can't verify this right now but: is it possible that it's running the validation twice? Once on the object that's passed to Model.update and once for each model being updated?

@Kraicheck
Copy link

No, validation only runs once; for the object built from the passed properties.
Afterwards the individual objects are retrieved, the individual hooks are run and if those have a different impact for the different objects, update is run on those individual objects but validation is disabled as it's assumed to have already run.

@ephys
Copy link
Member

ephys commented Feb 4, 2022

I see. That could have been intended but if indivualHooks is used, I think it makes more sense to run validation on each instance one by one.

Changing it in v6 could break some users but we're actively working on v7 right now so if someone wants to open a PR to change the behavior in v7 (main branch), we can include it before the first stable release

@github-actions
Copy link
Contributor

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

@github-actions github-actions bot added the stale label Feb 19, 2022
@WikiRik
Copy link
Member

WikiRik commented Feb 19, 2022

@ephys can you add a label to this so the bot won't close this?

@github-actions github-actions bot removed the stale label Feb 20, 2022
@ephys ephys added breaking change For issues and PRs. Changes that break compatibility and require a major version increment. type: bug labels Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change For issues and PRs. Changes that break compatibility and require a major version increment. type: bug
Projects
None yet
Development

No branches or pull requests

6 participants