Randi Pratama
Randi Pratama

Reputation: 112

Creating associations of a tables that both have composite keys

I have a problems in sequelize to create a relationship between to models that both have composite keys. Let me show you the tables.

enter image description here

Usually to create a relationship between to models in sequelize, we use belongsTo or hasMany etc, and then set the foreignKey to the primaryKey of the target table (e.g. toolsName || tools_name). In my case I need to create a relationship of historyTool that belongsTo tools, so the code will look like this.

models.ChecklistTool.belongsTo(models.Tools, {
          foreignKey: 'toolsName',
          as: 'Tools'
        }
);

Now, the problem is table tools set the clusterId as its another primaryKey which resulting a composite primary key for table (e.g. (glasses)(1)), I'm confused how to create the associate models.

While I realize it will be much easier if table tools has its own auto increment id (tools_id), it will be much help if I know how to create one without it, since it will be a good references in the future when I don't have any control in designing the tables.

Thank you so much, any help will be appreciated.

Upvotes: 0

Views: 219

Answers (1)

Randi Pratama
Randi Pratama

Reputation: 112

I just write it in SQL, use sequelize.query() method and get along with it.

Upvotes: 0

Related Questions