title: Sequelize create one-to-one and insert data into foreign key field?
body: <p>I am trying to create 2 tables with a one-to-one relationship.</p>
  <ul>
  <li>Orders (of which the id is the primary key)</li>
  <li>Expiries (which will refer to an order)</li>
  </ul>
  <p>Not all Orders will have an expiry, but all expiries will have an order.</p>
  <p>Using DB Browser for SQLite I can see the 2 tables have been created and <em>look</em> to be okay. All the data is entered into the Orders table without issue. However when attempting to add data to the Expiry table it is giving me a Foreign Key constraint error that is not very helpful:</p>
  <pre><code>{
    parent: [Error: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed] {
      errno: 19,
      code: 'SQLITE_CONSTRAINT',
      sql: 'INSERT INTO `SUP_expiries` (`id`,`merchantId`,`expiredDate`,`expiredAmt`,`createdAt`,`updatedAt`,`OrderId`) VALUES (NULL,$1,$2,$3,$4,$5,$6);'
    }
  }
  </code></pre>
  <p>I think it has something to do with me trying to add the Foreign Key values, but I cannot figure it out. I already have all of the Id's in the csv to link Expiries with Orders. Full code is below, any helps is much appreciated!</p>
  <pre><code>var fs = require('fs')
  var csv = require('csv-parser')
  var moment = require('moment')
  const {
      Sequelize,
      DataTypes,
      FLOAT
  } = require('sequelize')
  
  const sequelize = new Sequelize({
      dialect: 'sqlite',
      storage: 'data.db',
      logging: false,
      query: {
          raw: true
      }
  })
  
  const Order = sequelize.define('Order', {
      id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
      },
      merchantId: DataTypes.INTEGER,
      merchantName: DataTypes.STRING,
      orderAmt: DataTypes.INTEGER,
      orderDate: DataTypes.DATEONLY,
      expiryDate: DataTypes.DATEONLY
  }, {
      tableName: 'SUP_orders'
  })
  
  const Expiry = sequelize.define('Expiry', {
      merchantId: DataTypes.INTEGER,
      expiredDate: DataTypes.DATEONLY,
      expiredAmt: DataTypes.INTEGER,
  }, {
      tableName: 'SUP_expiries'
  })
  
  Order.hasOne(Expiry)
  Expiry.belongsTo(Order)
  
  function addOrders() {
      return new Promise((resolve) =&gt; {
          const data = []
          fs.createReadStream('SUP_orders.csv').pipe(csv()).on('data', (row) =&gt; {
              let orderDate = moment(row.order_date).format('YYYY-MM-DD')
              data.push({
                  id: row.order_id,
                  merchantId: row.merchant_id,
                  merchantName: row.merchant_name,
                  orderAmt: row.order_amount,
                  orderDate: orderDate,
                  expiryDate: moment(orderDate).add(19, 'days')
              })
          }).on('end', () =&gt; {
              Order.bulkCreate(data, {
                  validate: true
              })
              resolve()
          })
      })
  
  }
  
  function addExpiries() {
      return new Promise((resolve) =&gt; {
          fs.createReadStream('SUP_expiries.csv').pipe(csv()).on('data', (row) =&gt; {
              Expiry.create({
                  merchantId: row.merchant_id,
                  OrderId: row.order_id,
                  expiredDate: moment(row.expire_date).format('YYYY-MM-DD'),
                  expiredAmt: row.expired_sum
              })
          }).on('end', () =&gt; {
              resolve()
          })
      })
  }
  
  async function main() {
      try {
          await sequelize.sync({force: true})
          await addOrders()
          await addExpiries()
      } catch (err) {
          console.log(err)
      }
  }
  
  main()
  </code></pre>