0

hi i have table in MySql as user_role and it have two columns Userid (int) and RoleId (int) and the combination of both is a primary key now how can i represent that in sails.js model User_role.js you can see the model bellow

attributes: {
      UserId:{type:"number"},
      RoleId:{type:"number"}
}  

now I would like to add them both as a primary key so can any one help me with that.

Jasmin Raval
  • 184
  • 1
  • 5
  • 15
  • Does this answer your question? [Sails.js composite unique field](https://stackoverflow.com/questions/24923750/sails-js-composite-unique-field) – dusthaines Apr 24 '20 at 14:26
  • hi @dusthaines thanks for your comment actually value themselves are not unique the combination of them is unique so do you have any idea how we can do that – Jasmin Raval Apr 24 '20 at 14:44
  • What you are describing is called a [composite index](https://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html) in mysql. In your case you would structure it as a unique key. It is not advised to use composite index as a primary key. Based on the link I provided and additional research `Sails` models do not support composite index and you will need to create it directly in the DB (via Workbench, SequelPro, terminal, or similar). – dusthaines Apr 24 '20 at 15:06
  • thanks @dusthaines, okay is there any way to not removing waterfall orm and still remove primary key from model. if i don't want any attribute as primary key – Jasmin Raval Apr 24 '20 at 15:17
  • I don't work with `Sails` or `Waterline` so I can't help in a meaningful way. You'll have to do some additional research on the topic. This looks like a good place to start: https://stackoverflow.com/q/49736218/1720873 – dusthaines Apr 24 '20 at 16:20
  • May I ask, is this a join table just to connect Users to Roles? Or does it have a more complicated purpose. If it is a join table, Sails has a way to implement the join without explicitly defining a model for the join table. – arbuthnott Apr 24 '20 at 17:37
  • hi @arbuthnott in a way yes the table is to represent many-to-many relation ship – Jasmin Raval Apr 28 '20 at 10:06

0 Answers0