Returning option when create instance of Model don't work as expected.
Issue Creation Checklist
- [x] I understand that my issue will be automatically closed if I don't fill in the requested information
- [x] I have read the contribution guidelines
Bug Description
Reproducible Example
Here is the link to the SSCCE for this issue: https://github.com/olegdobrynin/avier-server/blob/main/src/controllers/artist.js
What do you expect to happen?
Hi,
I wrote server, it has sequelize and Postgres. When I create an instance of Model, returning doesn't work:
What I do:
const artist = await Artist.create(
{ name, bio, user_id: userId, img: imgName },
{ returning: ['id', 'name', 'img'], transaction },
);
console.log(artist.toJSON());
Expected:
{
id: 39,
name: 'sample',
img: 'default.jpg'
}
What is actually happening?
SQL Query:
INSERT INTO "artists" ("id","name","user_id","bio","img","created_at","updated_at")
VALUES (DEFAULT,$1,$2,$3,$4,$5,$6) RETURNING "id","name","img";
What is logged:
{
id: 39,
name: 'sample',
bio: '',
user_id: '8',
img: 'default.jpg',
updated_at: 2022-07-01T15:14:03.442Z,
created_at: 2022-07-01T15:14:03.442Z
}
Logging instance of Model has the same amount of dataValues as toJSON method.
Environment
- Sequelize version: v6.21.2
- Node.js version: v18.4.0
- Database & Version: PostgreSQL v14.4
- Connector library & Version: pg v8.7.3, pghstore v2.3.4
Would you be willing to resolve this issue by submitting a Pull Request?
- [ ] Yes, I have the time and I know how to start.
- [x] Yes, I have the time but I will need guidance.
- [ ] No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- [ ] No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
This works as expected. returning is used to fetch back data generated by the database into the model. It doesn't delete local model data
Can you explain a bit?
I understand that models as constructors has different methods. My server doesn't save any models instances. They are stored as rows in my DB. I thought that models only needs to insert or find some data from db, not for storing values. Do you mean model is:
User {
dataValues: {
id: 19,
login: 'gomez',
role: 'admin',
password: '$2b$05$yzLxfoMu/8yUscj7VIq5pOg9wNMmPCZ4C7vIzeMkg086Snv35yVpe',
updated_at: 2022-07-01T20:20:42.737Z,
created_at: 2022-07-01T20:20:42.737Z
},
_previousDataValues: {
login: 'gomez',
role: 'admin',
password: '$2b$05$yzLxfoMu/8yUscj7VIq5pOg9wNMmPCZ4C7vIzeMkg086Snv35yVpe',
id: 19,
created_at: 2022-07-01T20:20:42.737Z,
updated_at: 2022-07-01T20:20:42.737Z
},
uniqno: 1,
_changed: Set(0) {},
_options: {
isNewRecord: true,
_schema: null,
_schemaDelimiter: '',
attributes: undefined,
include: undefined,
raw: undefined,
silent: undefined
},
isNewRecord: false
}
If I insert values in table in pg output is:
insert into users ("login", "password") values ('hello', 'world') returning "login", "password";
login | password
-------+----------
hello | world
(1 row)
Without returning it returns nothing:
insert into users ("login", "password") values ('hello', 'world');
INSERT 0 1
I expect that returning in sequelize will work as in pg and if not defined it returns all values, like attributes in find methods.
Sequelize api says:
Appends RETURNING
to get back all defined values; if an array of column names, append RETURNING to get back specific columns (Postgres only)
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. 🙂
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. 🙂