how to define the relations between 3 tables
See original GitHub issueI want to create a relations between 3 tables with 1 table, like:
here is my 3 tables write with sequelize models. case:
export default (sequelize, DataTypes) => {
return sequelize.define('Case', {
case_id: {type: DataTypes.INTEGER, field: 'case_id', primaryKey: true},
name: {type: DataTypes.STRING(100), defaultValue: '', field: 'name'},
code: {type: DataTypes.STRING(100), defaultValue: '', field: 'code'},
}, {
timestamps: false,
tableName: 'T_CASE'
})
}
caseRole:
export default (sequelize, DataTypes) => {
return sequelize.define('CaseRole', {
role_id: {type: DataTypes.INTEGER, field: 'role_id', primaryKey: true},
name: {type: DataTypes.STRING(100), defaultValue: '', field: 'name'}
}, {
timestamps: false,
tableName: 'T_CASE_ROLE_DEF'
})
}
user:
export default (sequelize, DataTypes) => {
return sequelize.define('User', {
user_id: {type: DataTypes.INTEGER, field: 'user_id', primaryKey: true},
sid: {type: DataTypes.STRING(100), defaultValue: '', field: 'sid'},
login_name: {type: DataTypes.STRING(100), defaultValue: '', field: 'login_name'}
}, {
timestamps: false,
tableName: 'T_USER'
})
}
I want to join these 3 tables with 1 table like:
T_CASE_ROLE_USERS
字段名 | 类型 | 备注 |
---|---|---|
case_id | number | |
role_id | number | |
user_id | number |
and I want to know how to write associate
method in my sequelize models, thks!!
Dialect: mysql __Database version: 5.7.15 __Sequelize version: 3.24.3
Issue Analytics
- State:
- Created 7 years ago
- Comments:15 (6 by maintainers)
Top Results From Across the Web
Define relationships for 3 tables - DBA Stack Exchange
Based on your description you want to have a GroupMembers table that does all the many to many joins for you.
Read more >Creating multiple tables and table relationships - Launch School
In this chapter we'll explore the reasons for having multiple tables in a database, look at how to define relationships between different tables, ......
Read more >Relationships between tables in a Data Model
To create a relationship between two tables that have multiple columns defining the primary and foreign keys, first combine the values to create...
Read more >How to Join 3 Tables (or More) in SQL - LearnSQL.com
In the final part, we'll have to join all the tables together. The first task is to choose the table which will go...
Read more >Creating a relationship between three tables - Stack Overflow
Of course, if you defined your junction table with PRIMARY KEY (product_id, pricelist_id) , this table would lack the ability to uniquely define...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
All documented here: http://docs.sequelizejs.com/en/v3/docs/associations/#belongs-to-many-associations
Well the queries will be very complex, but with include you should be able to do what you need: