Skip to content

postgres .save() not able to detect change in Array fields #13833

Closed

Description

@rajivharlalka

Issue Creation Checklist

Bug Description

Changes in array fields are not getting detected and hence does not update database

SSCCE

const { Model, DataTypes } = require(".");

const sequelize = createSequelizeInstance({ benchmark: true });

const Student_Model = function (sequelize, DataTypes) {
    return sequelize.define(
        "Student",
        {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                primaryKey: true,
            },
            certificates: {
                type: DataTypes.ARRAY(DataTypes.UUID),
                defaultValue: [],
            },
        },
    );
};

const Certificate = Certificate_Model(sequelize, Sequelize);

(async () => {
  await sequelize.sync({ force: true });

 const student_instance = await Student.findOne({where: {email}});

    student_instance.certificates.push(certificate_instance.id);
    await student_instance.save();
})();

What do you expect to happen?

I wanted the array to get updated in the table of database

What is actually happening?

No change in the row of the table

Additional context

Certificate instance has a field location which I need to append everytime an endpoint is hit

Environment

  • Sequelize version: 6.12.0
  • Node.js version: 14.18.0
  • If TypeScript related: NO

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): Postgres
  • 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.

Activity

  1. self-assigned this
    on Dec 28, 2021
  2. ephys commented on Dec 28, 2021

    @ephys
    Member

    Changes are detected when you assign something to a property. Because you push to student_instance.certificates, student_instance.certificates is not reassigned and the change is not detected.

    You can force Sequelize to acknowledge the change by doing student_instance.changed('certificates', true)

  3. ephys commented on Dec 29, 2021

    @ephys
    Member

    Closing as answered. Let us know if it still doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions