Sequelize v3 — how do I make the default value for a hasMany relationship an empty list?
<p>I am using <a href="http://docs.sequelizejs.com" rel="nofollow">Sequelize</a> as my ORM. I have a simple <code>User</code> model as follows. A <code>User</code> <code>hasMany</code> <code>comments</code>, and a <code>Comment</code> <code>belongsTo</code> a <code>user</code>.</p>

<p><code>models/user.js</code> (trimmed to what's relevant)</p>

<pre><code>const model = (sequelize, DataTypes) =&gt; {

  const User = sequelize.define('User', {
      name: {
        type: DataTypes.STRING
      }
    },
    {
      classMethods: {
        associate: function(models) {
          User.hasMany(models.Comment, {as: 'comments'})
        }
      }
    }
  )

  return User
}

module.exports = model
</code></pre>

<p><code>models/comment.js</code> (also trimmed)</p>

<pre><code>const model = (sequelize, DataTypes) =&gt; {

  const Comment = sequelize.define('Comment', {
      text: {
        type: DataTypes.TEXT
      }
    }, {
      classMethods: {
        associate: function(models) {
          Comment.belongsTo(models.User, {as: 'author'})
        }
      }
    }
  )

  return Comment
}

module.exports = model
</code></pre>

<p>I've created a test as follows (trimmed for brevity)</p>

<pre><code>describe('create user given simple valid data', () =&gt; {
  const userData = {
    name: 'test'
  }

  it('creates a user', (done) =&gt; {
    User.create(userData, {
      include: [{model: models.Comment, as: 'comments'}]
    }).then((user) =&gt; {
      const userJS = user.get({plain: true})
      expect(userJS).to.have.property('name')
      expect(userJS).to.have.property('comments')
      done()
    }, done)
  })
})
</code></pre>

<p>which results in</p>

<pre><code>Unhandled rejection AssertionError: expected { Object (name, ...) } to have a property 'comments'
</code></pre>

<p>If I specify my <code>userData</code> like this however</p>

<pre><code>const userData = {
  name: 'test',
  comments: []
}
</code></pre>

<p>the test passes just fine.</p>

<p>How can I tell Sequelize to just make an empty list of comments as the default?</p>