不要怂,就是干,撸起袖子干!

DefaultValues.ejs 1.93 KB
<script type="text/javascript" charset="utf-8">
  document.observe("dom:loaded", function() {
    buildNavigation([], ["<br />", "<a href=\"/examples/Count\">Count</a>",
"<a href=\"/examples/DefaultValues\">Default values</a>",
"<a href=\"/examples/fetchAssociations\">fetchAssociations</a>",
"<a href=\"/examples/MethodPassing\">MethodPassing</a>",
"<a href=\"/examples/Performance\">Performance</a>",
"<a href=\"/examples/SequelizeWithOptions\">SequelizeWithOptions</a>",
"<a href=\"/examples/ChainQueries\">Using the chainQueries function</a>",
"<a href=\"/examples/UsingMultipleModelFiles\">UsingMultipleModelFiles</a>",
"<a href=\"/examples/Associations\">Working with associations</a>"], { seperator: ' ' })
  })
</script>

<div>
  <a name="sequelize"></a>
  <h1>Default values</h1>
  <p></p>
</div>


  <div class="seperator"></div>

  <div>
    <a name="app.js"></a>
    <h2>app.js</h2>
    <p>
      This example demonstrates the use of default values for defined model fields. Instead of just specifying the datatype,
  you have to pass a hash with a type and a default. You also might want to specify either an attribute can be null or not!
      <pre>

var Sequelize = require(__dirname + &quot;/../../lib/sequelize/Sequelize&quot;).Sequelize,
    sequelize = new Sequelize(&quot;sequelize_test&quot;, &quot;root&quot;, null),
    User      = sequelize.define('User', {
      name: { type: Sequelize.STRING, allowNull: false},
      isAdmin: { type: Sequelize.BOOLEAN, allowNull: false, default: false }
    }),
    user      = new User({ name: 'Someone' })
    
Sequelize.chainQueries([{drop: User}, {sync: User}], function() {
  user.save(function(user) {
    Sequelize.Helper.log(&quot;user.isAdmin should be the default value (false): &quot; + user.isAdmin)
    
    user.updateAttributes({ isAdmin: true }, function(user) {
      Sequelize.Helper.log(&quot;user.isAdmin was overwritten to true: &quot; + user.isAdmin)
    })
  })
})</pre>
    </p>
</div>