Skip to content

Instance.update is calling Model.create #9272

Closed

Description

@ryanmcclure4

What are you doing?

I am calling .update on a sequelize model instance and it is in turn attempting to insert a new record with the provided fields. I cannot remember which version this started happening, but it seemed one day all of my .update calls triggered an insert query. Model.update, however works fine.

const user = await User.findOne({ ... });

// This triggers a `INSERT INTO "users" ("field") VALUES("thing") ...`
user.update({ field: 'thing' });

// This updates the model just fine
User.update({ field: 'thing' }, { where: { id: 1 } });

What do you expect to happen?

The instance model should be updated

What is actually happening?

An INSERT query is called rather than an UPDATE query

Activity

  1. sushantdhiman commented on Apr 6, 2018

    @sushantdhiman
    Contributor

    "One day all of my .update calls triggered an insert query" not much to work with

    This could happen when isNewRecord: true or model missing id(primary key). I need a proper SSCCE to reproduce and tag this as a bug

    Please use Github Issue Tracker only for reporting bugs, requesting new features or discussions. Ask questions on Stackoverflow sequelize.js tag or Slack. While I would like to help answer any questions but it still take too much time.

    If you are reporting a bug please isolate it as SSCCE, present it nicely and follow issue template. This will help maintainers and contributors understand your issue quickly.

    If you are requesting a feature spend some time to properly present your use case with some examples, current and expected outcome.

    ( What is SSCCE ? You can find template for Sequelize here )

    A few good example of SSCCE

    1. ResourceRequest timed out - connection not reestablished after db restarted #8014 (comment)
    2. removeAttribute('id') results in undefined: null data value #7318 (comment)
    3. toJSON converting booleans to "t" (true) or "f" (false) #8749 (comment)
  2. ryanmcclure4 commented on Apr 6, 2018

    @ryanmcclure4
    Author

    I figured this would be an issue others were experiencing due to a recent version change, however I realized it is because of a change I made to the caching layer in my app. I am serializing the instance data and caching that, and then calling Model.build(data) after fetching from the cache. I then call update on this instance, so this must be causing the issue as build sets isNewRecord: true.

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

Metadata

Metadata

Assignees

No one assigned

    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