-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequelize-associations.txt
74 lines (46 loc) · 1.98 KB
/
sequelize-associations.txt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
Associations
(Detailed documentation http://docs.sequelizejs.com/en/latest/docs/associations/)
When calling a method such as User.hasOne(Project), we say that the User model (the model that the function is being invoked on) is the source and the Project model (the model being passed as an argument) is the target.
Let's have these defined for the entire association explanation list.
var Post = sequelize.define('Post', {});
var User = sequelize.define('User', {});
One-to-one associations
BelongsTo
Foreign key on the SOURCE model
Post.belongsTo(User); // foreign key added to Post with the name userId
// Post will have .getUser and .setUser methods
HasOne
Foreign key on the TARGET model
Post.hasOne(User); // foreign key added to User with the name postId
// Post will have the following methods Post.getUser and Post.setUser methods
One-to-many associations
Source with multiple targets and targets have one source.
User.hasMany(Post); // foreign key added to post with name userId.
// User will have the following methods
// .addPost, .addPosts
// .getPosts
// .createPost
// .countPosts
// .hasPost
// .hasPosts
// .removePost
// .removePosts
// .setPosts
Many-to-many associations
BelongsToMany
Used to connect sources with multiple targets and targets can have multiple sources.
// Creates a new model with name UserPost for the join table, which will have userId and postId foreign keys.
// The through definition is required.
User.belongsToMany(Post, {through: 'UserPost', as: 'Likes'});
Post.belongsToMany(User, {through: 'UserPost', as: 'Likes'}});
// Both User and Post will have the following methods
// .getLikes
// .setLikes
// .addLike
// .addLikes
// .removeLikes
// .removeLikes
// .hasLikes
// .hasLike
// .createLike
// .countLikes