Skip to content

Sequelize model is trying to insert null where value is not null #11417

Closed

Description

@tareqtms

Issue Description

What are you doing?

//So here is my Sequelize model, which is a join table between the tables shows and tags (I commented out the associations to make sure that the issue is not with associations):

    module.exports = (sequelize, DataTypes) => {
      const ShowsTag = sequelize.define('ShowsTag', {
        showId: {
          type: DataTypes.BIGINT,
          allowNull: false,
          field: 'show_id'
        },
        tagId: {
          type: DataTypes.BIGINT,
          allowNull: false,
          field: 'tag_id'
        },
        tagConnectType: {
          type: DataTypes.STRING,
          allowNull: true,
          field: 'tag_connect_type'
        }
      }, {
        tableName: 'shows_tags',
        timestamps: true,
        underscored: true
      });
      ShowsTag.associate = function(models) {
        // associations can be defined here
        //models.ShowsTag.belongsTo(models.Tag, {as: 'tag'});
        //models.ShowsTag.belongsTo(models.Show, {as: 'show'});
      };
      return ShowsTag;
    };

 //Here is the code where I am creating a new record:

    db.ShowsTag.create({showId: 3, tagId: 3, tagConnectType: 'channel'}).then(d =>
      console.log(d)
    )

What do you expect to happen?

It should insert with showId = 3 and tagId = 3.

What is actually happening?

It's sending to database, showId = null and tagId = null.

_The output was: (Note that show_id is equivalent to showId, as it is an underscored model)

Executing (default): INSERT INTO "shows_tags" ("show_id","tag_id","tag_connect_type","created_at","updated_at") VALUES (NULL,NULL,'channel','2019-09-08 21:51:33.930 +06:00','2019-09-08 21:51:33.930 +06:00') RETURNING *;
Unhandled rejection SequelizeDatabaseError: null value in column "show_id" violates not-null constraint

Additional context

Add any other context or screenshots about the feature request here.

Environment

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect. (I am using Postgres)

Would you be willing to resolve this issue by submitting a Pull Request?

  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. bclark-systech commented on Sep 17, 2019

    @bclark-systech

    Having the same problem. I did find a workaround by passing in the fields option. Not sure if this is a bug or a feature.

    db.OrganizationContact.create(
         { organizationId: 123, accountContactId: 456 },
         { fields: ["organizationId", "accountContactId"] }
    );
    
  2. self-assigned this
    on Sep 17, 2019
  3. added
    existing workaround
    type: bug
    on Sep 17, 2019
  4. malko42 commented on Oct 16, 2019

    @malko42

    I have the same issue with
    Sequelize 4.44.3
    Node 12.4.0
    OS Arch linux 5.3.6

    And strangely enough for a Tags model too.

    I can confirm the workaround works.

  5. gon-escalante commented on Dec 26, 2019

    @gon-escalante

    adding the primaryKey attribute to one of the fields in model solved this issue for me, there seems to be a problem with the underscored fields

  6. papb commented on Jan 16, 2020

    @papb
    Member

    Closing in favor of #11225

  7. removed their assignment
    on Jan 16, 2020
  8. normandanielmq commented on Nov 8, 2022

    @normandanielmq

    When i faced this issue, there was a misconfiguration between the tables. Take a look at how they are related in the models.

  9. ephys commented on Nov 8, 2022

    @ephys
    Member

    Can you open a new issue with the information the template will request and a SSCCE/MVCE?

  10. fhyfr commented on Feb 18, 2023

    @fhyfr
  11. tovaru commented on Jun 23, 2023

    @tovaru

    I had this issue because I was using super many-to-many, where I defined both hasMany and belongsToMany between tables.

    I solved it by changing the order in which I define relations; by setting belongsToMany before hasMany.

  12. egmontaska commented on Jul 13, 2023

    @egmontaska

    Had this issue in many-to-many relation while models having property underscored: true. Resolved by adding foreignKey property to association definition.
    Before:

    User.belongsToMany(Company, {
      through: CompanyUser,
      as: 'companies'
    })
    
    Company.belongsToMany(User, {
      through: CompanyUser,
      as: 'users'
    })
    

    After:

    User.belongsToMany(Company, {
      through: CompanyUser,
      as: 'companies',
      foreignKey: 'user_id'
    })
    
    Company.belongsToMany(User, {
      through: CompanyUser,
      as: 'users',
      foreignKey: 'company_id'
    })
    
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

    existing workaroundtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions