sequelize / sequelize/sequelize

[v6] defaultScope is not working for upsert and update

Open
#12,478 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Description

I add defaultScope in model definition to exclude some columns. But .upsert() still return the exclude columns.

What are you doing?

Here is the link to the SSCCE for this issue: LINK-HERE

const Sequelize = require('sequelize')

const sequelize = new Sequelize('postgres://postgres:password@localhost:5432/postgres')
const Test = sequelize.define(
  'test',
  {
    tableId: { type: Sequelize.STRING, primaryKey: true },
    owner: { type: Sequelize.STRING, primaryKey: true },
    type: Sequelize.STRING,
  }, {
    defaultScope: { attributes: { exclude: ['type'] } }
  }
)

;(async () => {
  await sequelize.query('DROP TABLE IF EXISTS tests CASCADE')
  await Test.sync()
  const [test] = await Test.upsert({ tableId: 'aa', owner: 'bb', type: 'cc' }, { returning: true })

  console.log(test.toJSON())
})()
What do you expect to happen?

type should be excluded.

What is actually happening?

The output was Bar!

{ tableId: 'aa',
  owner: 'bb',
  type: 'cc',
  createdAt: 2020-07-09T10:39:34.770Z,
  updatedAt: 2020-07-09T10:39:34.770Z }
Additional context

Add any other context or screenshots about the feature request here.

Environment
  • Sequelize version: 6
  • Node.js version: 10
  • Operating System: macOS
  • If TypeScript related: TypeScript version: XXX

Issue Template 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Sequelize model and the Test.upsert call using defaultScope attributes.exclude; compare how upsert and update apply scopes to returned attributes. Add regression coverage for excluding type and verify the returned object no longer includes it.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, postgresql
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.