DefaultValues.ejs
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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 + "/../../lib/sequelize/Sequelize").Sequelize,
sequelize = new Sequelize("sequelize_test", "root", 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("user.isAdmin should be the default value (false): " + user.isAdmin)
user.updateAttributes({ isAdmin: true }, function(user) {
Sequelize.Helper.log("user.isAdmin was overwritten to true: " + user.isAdmin)
})
})
})</pre>
</p>
</div>