Skip to content

Instantly share code, notes, and snippets.

@dylansalim3
Created September 13, 2020 15:11
Show Gist options
  • Save dylansalim3/eca6cc0e6a90f51edd776a5922906a00 to your computer and use it in GitHub Desktop.
Save dylansalim3/eca6cc0e6a90f51edd776a5922906a00 to your computer and use it in GitHub Desktop.
Sequelize Basics
//One to One
bookDetail.hasOne(category);
category.belongsTo(bookDetail);
//One to Many
borrowBook.belongsTo(user,{foreignKey: 'user_id',});
user.hasMany(borrowBook,{foreignKey: 'user_id',});
//Many to Many
bookDetail.belongsToMany(author,{through: "book_author",foreignKey:'book_detail_id'});
author.belongsToMany(bookDetail, {through: "book_author",foreign_key:'author_id'});
//Force rewrite the db with new structure (WILL TRUNCATE ALL DB TABLE AND RECREATE THEM)
db.sequelize.sync({force:true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment