title: Sequelize One to Many with Null Reference
body: <p>My scenario is like this. For a Given Stock Item there will be zero or more costs associated with it. So fay my Sequelize models are looks as follows.</p>
  
  <pre><code>  // StockItem Model
    const StockItem =  sequelize.define('StockItem', {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      purchasedDate: {
        type: DataTypes.DATE,
        allowNull: true,
        defaultValue: null,
        field: 'purchased_date',
      }, {
      // Other attributes
      }, {
      paranoid: false,
      createdAt: 'created_date',
      updatedAt: 'updated_date',
      tableName: 'STOCK_ITEMS'
      });
       StockItem.associate = (models) =&gt; {
             StockItem.hasMany(models.Cost, { as: 'StockItemCosts', foreignKey: 'stock_item_id' });
       };
    });
  
    // Cost Model
      const Cost = sequelize.define('Cost', {
       id: {
         type: DataTypes.INTEGER,
         autoIncrement: true,
         primaryKey: true,
       },
       type: {
         type: DataTypes.STRING(50),
         allowNull: false,
       }, {
        // Other attributes
       }, {
        paranoid: false,
        createdAt: 'created_date',
        updatedAt: 'updated_date',
        tableName: 'COSTS'
      });
    });
  </code></pre>
  
  <p>As per my requirements I should be able to save a Stock Item even though it has no cost. I tried different ways of doing this. But I'm getting following error when I'm trying to save a cost will a null value. I use express js based Rest API to save my data to MySQL database. And I have a saving stock item data to the db as follows:</p>
  
  <pre><code> const stockItem = {
    ...restApiData // Data received from FE form submission.
    StockItemCosts: restApiData.costDetails, // This is null when there is no costs associated with the item.
   }
   sequelize.transaction((t) =&gt; {
      return  StockItem.create(stockItem, {
        transaction: t,
        include: [ { model: Cost, as: 'StockItemCosts' }]
      });
    }).then ((result) =&gt; {
     // console.log('result : ', result); // eslint-disable-line no-console
  
     // Transaction has been committed
      // result is whatever the result of the promise chain returned to the transaction callback
    }).catch((err) =&gt; {
     console.log('err : ', err); // eslint-disable-line no-console
      // Transaction has been rolled back
      // err is whatever rejected the promise chain returned to the transaction callback
    });
  </code></pre>
  
  <p>And this is the error I'm getting.</p>
  
  <pre><code>Executing (83cc74a4-7067-44ef-8767-d0c08783a132): ROLLBACK;
  err :  { SequelizeValidationError: notNull Violation: Cost.type cannot be null,
  notNull Violation: Cost.cost cannot be null,
  notNull Violation: Cost.createdBy cannot be null,
  notNull Violation: Cost.updatedBy cannot be null
  </code></pre>
  
  <p>I tried constraints: false as follows but that also did not work. Any help much appreciated. </p>
  
  <p>StockItem.hasMany(models.Cost, { as: 'StockItemCosts', foreignKey: 'stock_item_id', constraints: false });</p>
  
  <p>--------------------------------------------------------------- Update -------------------------------------------------------</p>
  
  <p>I found a solution as per the @Praveen Kumar`s comment. But when I save the entities without the cost I'm unable to fetch those records which do not have associated costs. As I'm fetching data with pagination I'm unable to have different queries to get it done. </p>
  
  <p>This is how I'm doing now :</p>
  
  <pre><code>return StockItem.findAll({ include: [ { model: Cost, as: 'StockItemCosts' }] }) 
  </code></pre>