Make create to return with scope
See original GitHub issueI have this model
module.exports = (sequelize, DataTypes) => {
const Partner = sequelize.define('Partner', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'partners',
paranoid: true,
underscored: true,
freezeTableName: false,
scopes: {
clean: {
attributes: { exclude: [
'createdAt',
'updatedAt',
'deletedAt'
] }
}
}
})
return Partner
}
When doing a Partner.scope('clean').find()
, the query runs
SELECT "id", "name", "logo_id" AS "logoId" FROM "partners" AS "Partner" [...]
Whereas doing a Partner.scope('clean').create()
runs
INSERT INTO "partners" ("id","name","created_at","updated_at") VALUES (DEFAULT,$1,$2,$3) RETURNING *
So it doesn’t respect the scope.
I think it would be very more consistent (and actually, relatively easy) to tweak the RETURNING *
part automatically to respect scopes ?
Furthermore, I can’t even do this manually, becasecreate()
returning
option is only boolean (whereas bulkCreate
can take an array of columns)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Scope and return values in C++
The scope of the local variable ends, but a copy is made, and returned to the calling function. Example: int funcB() { int...
Read more >JavaScript Functions — Understanding Scope & the Return ...
In this post, we will focus on 2 additional concepts, which are really important for understanding functions: scope and the return statement.
Read more >Make query scope only return one object
Hello! I created a query scope to return the current promotion. It looks like this: public function scopeCurrent($query, array $fields = null) {...
Read more >Scope and Closures in JavaScript – Explained with ...
First, we can access word in the function returned from sayWord. ... Thus, creating a variable in the global scope makes it accessible...
Read more >Scope functions
Although the scope functions are a way of making the code more ... the return value and use a scope function to create...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @papb
Yes about the original issue; but actually yes and no. Yes for the insert / returning of Postgres. But I’m thinking more broadly about
UPDATE
/.reload()
now. What do you think ?One should produce failing tests maybe.
Hum… I didn’t find anything in the docs mentionning any of this… (I had to actually test it)
So… It’s a “breaking” change in the mean it should break stuff; but SEMVER-speaking, no…
Ninja commiting it would break stuff for sure, and I think it can be added as a breaking change.
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. 🙂