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

associations.ejs 1.18 KB
With Sequelize you can also specify associations between multiple classes. Doing so will help you to easily 
access and set those associated objects. The library therefore provides for each defined class the following methods:<br/><br/>

NOTE: Associations with models that use custom primaryKeys (so not the field 'id') are currently unsupported.

<pre><%- koala(".js", partial("code/associations/has-one.ejs")) %></pre>
<pre><%- koala(".js", partial("code/associations/belongs-to.ejs")) %></pre>
<pre><%- koala(".js", partial("code/associations/has-many.ejs")) %></pre>

Because Sequelize is doing a lot of magic, you have to call Sequelize#sync after setting the associations!
Doing so will allow you the following:

<pre><%- koala(".js", partial("code/associations/setter-and-getter.ejs")) %></pre>

To remove created associations you can just call the set method without a specific id:

<pre><%- koala(".js", partial("code/associations/single-add-remove.ejs")) %></pre>

You can of course also do it vice versa:

<pre><%- koala(".js", partial("code/associations/vice-versa.ejs")) %></pre>

For hasOne its basically the same:

<pre><%- koala(".js", partial("code/associations/has-one-example.ejs")) %></pre>