Skip to content

model.get() does not clone objects for JSON data-type #10052

@jusguy

Description

@jusguy

What are you doing?

const Sequelize = require('sequelize')

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: ':memory:'
})

const Album = sequelize.define('User', {
  name: Sequelize.TEXT,
  attributes: Sequelize.JSON
})

Album.sync().then(() => {
  return Album.create({
    name: 'Slow Dance Showdown',
    attributes: {
      artist: 'john smith',
      genre: 'rock'
    }
  })
}).then(album => {
  const dataCopy = album.toJSON()

  dataCopy.attributes.genre = 'classical'
  dataCopy.name = 'Workout Jams'

  console.log('Name compare: ' + (dataCopy.name === album.name)) // works properly
  console.log('Attributes compare: ' + (dataCopy.attributes === album.attributes)) // does not work properly
})

What do you expect to happen?

Using JSON data-type I expect model.get() and model.toJSON() to return a copy of the object.

What is actually happening?

Instead of a copy of the object representing the JSON you get a reference to the same object that the model uses.

Dialect: mysql / postgres / sqlite
Dialect version: various
Database version: various
Sequelize version: master branch
Tested with latest release: Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueFor issues. An issue that is a good choice for first-time contributors.type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions