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

has-one.ejs 908 Bytes
var User = sequelize.define('User', {/* ... */})
var Project = sequelize.define('Project', {/* ... */})

// One-way associations
Project.hasOne(User)

/*
  In this example hasOne will add an attribute ProjectId to the User model!
  Furthermore, Project.prototype will gain the methods getUser and setUser according
  to the first parameter passed to define. If you have underscore style 
  enabled, the added attribute will be project_id instead of ProjectId.
  
  You can also define the foreign key, e.g. if you already have an existing
  database and want to work on it:
*/
  
Project.hasOne(User, { foreignKey: 'initiator_id' })
  
/*
  Because Sequelize will use the model's name (first parameter of define) for
  the accessor methods, it is also possible to pass a special option to hasOne:
*/

Project.hasOne(User, { as: 'Initiator' })
// Now you will get Project#getInitiator and Project#setInitiator