javascript - One-to-one relationship with Sequelize -


i creating twitter clone fun, , having issues sequelize relationship side of things.

my objective create many-to-one relationship for: user-to-tweets

and create one-to-one relationship for: tweet-to-user

i have completed many-to-one relationship in tweet.js file, can see here:

'use strict'; module.exports = function(sequelize, datatypes) {   var tweet = sequelize.define('tweet', {     tweet: datatypes.text   }, {     classmethods: {       associate: function(models) {         // associations can defined here         //creating one-to-many relationship user->tweets         models.user.hasmany(models.tweet, {as: 'tweets'});       }     }   });   return tweet; }; 

however, in user.js intend create one-to-one relationship from: tweet-to-user, having issues.

i tried following documentation here, cannot relationship create successfully. here example of current code:

'use strict'; module.exports = function (sequelize, datatypes) {   var user = sequelize.define('user', {     username: datatypes.string,     password: datatypes.string   }, {       //classmethod guarantees password provided user matches what's in db       classmethods: {         associate: function (models) {           //associate every tweet having 1 author (user)           models.tweet.hasone(models.user, { as: "author" });           models.tweet#setauthor(anauthor);         }       }     });   return user; }; //**note 'models' contains 2 objects: 'tweet' , 'user'** 

i'm not sure how handle relationship, how can create one-to-one relationship tweet-to-user?

i think, maybe you:

       module.exports = function(sequelize, datatypes) {             var tweet = sequelize.define('tweet', {             tweet: datatypes.text               }, {                 classmethods: {                    associate: function(models) {                    tweet.belongsto(models.user, {as: 'tweets'})               }            }       });        return tweet;   };        module.exports = function (sequelize, datatypes) {          var user = sequelize.define('users', {                    username: datatypes.string,                    password: datatypes.string            }, {     //classmethod guarantees password provided user matches     what's in db            classmethods: {                    associate: function (models) {                    user.hasmany(models.tweet, {as: "author"})                   }             }         });            return user;      }; 

Comments