Skip to content

Updating instance of a buffer does not persist Blob #13801

Closed

Description

@garma83

Issue Creation Checklist

[x ] I have read the contribution guidelines

Bug Description

When you update a Buffer in memory that is stored with sequelize as a Blob, the updated buffer is not saved when you save the model.

SSCCE

    const CacheMap = sequelize.define("cachemap", {
        mapWidth: {
            type: Sequelize.INTEGER,
            defaultValue: 512,
        },
        mapLength: {
            type: Sequelize.INTEGER,
            defaultValue: 512,
        },
        data: {
            type: Sequelize.BLOB('medium'),
        },
    });

    CacheMap.beforeCreate(async (cachemap, options) => {
// this works
        cachemap.data = Buffer.alloc(cachemap.mapWidth * cachemap.mapLength, 0);
    });

// init the map, make the change

await CacheMap
                        .create({
                            mapWidth: 5,
                            mapLength: 5,
                        })

// change value to 12 at offset 3
map.data.writeInt8(12 ,4)
// logging the result to console gives me the correct change
// <Buffer 00 00 00 00 00 00 0c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>

map.save();

What do you expect to happen?

// retrieve data
CacheMap
            .findAll({
                where: {}
            })
//data would be saved as follows:
<Buffer 00 00 00 00 00 00 0c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>

What is actually happening?

//data in the database looks like this:
<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>

Additional context

I think the reason is that Sequelize might decide whether to update the blob based on the reference, not based on whether the data is actually changed.

Environment

  • Sequelize version: latest
  • Node.js version: latest

Bug Report Checklist

How does this problem relate to dialects?

  • [x ] 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...
  • [x ] No, I don't have the time and I wouldn't even know how to start.

Activity

  1. github-actions commented on Jan 5, 2022

    @github-actions
    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. 🙂

  2. garma83 commented on Jan 5, 2022

    @garma83
    Author

    still an issue

  3. self-assigned this
    on Jan 5, 2022
  4. ephys commented on Jan 5, 2022

    @ephys
    Member

    Sequelize needs you to assign something to map.data to detect changes. Solution is to call map.changed('map', true) to force the model to consider that field dirty.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions