question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to define the relations between 3 tables

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

29reactions
felixfbeckercommented, Oct 8, 2016
Case.belongsToMany(User, {through: CaseRole});
User.belongsToMany(Case, {through: CaseRole});

All documented here: http://docs.sequelizejs.com/en/v3/docs/associations/#belongs-to-many-associations

7reactions
alitahericommented, Oct 8, 2016

Well the queries will be very complex, but with include you should be able to do what you need:

Case.findAll({
  include: [{
    model: CaseRoleUser,
    include: [{ model: Role }, {model: User }],
  }],
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found