i'm having hard times understanding how I should model many-to-many relationships in objections.js.
Basically I have an app with an Users, Games and UsersScore tables. A account can have score in game.
Tables are like this:
USERS TABLE
- id
- username
GAMES TABLE
- id
- name
UsersScore TABLE
- id
- userId
- gameId
- score
How do I create the relation?
I want to show the logged in user a list of all the games And if he has score in the game then his score
Thanks!!
This is essentially identical to the requirements outlined at Join table extra properties in the Objection documentation. You could adapt this to create
users
,games
, andusers_games
tables. Then yourUser
model might look like this:Here you can see that we've added an extra
score
column to ourusers_games
join table. Then I imagine you would get a list of games with something like:See the documentation page for an example of how to construct the migrations.