Reputation: 897
Can I create compound primary key in RethinkDB ? For example if I have table with next documents strucure {authorsId: '371df80c-2efd-48f9-ac37-3edcdb152fb1', postsId: '905e0c92-adcb-4095-98df-fa08ca71b900'}
how can I create primary key on both together authorsId
and postsId
. Or if I cannot do this how should I use many to many relationship in three tables way. Have I use one field as primary key and second as secondary index ?
Upvotes: 6
Views: 2903
Reputation: 13031
Primary keys can only be on one column but you can put postsId
and authorsId
in an array and use that as primary key value.
See here for more information: https://www.rethinkdb.com/api/javascript/table_create/
EDIT
The primary key is still just one field but you can use [postsId, authorsId] as the value for that column and achieve the same. (added back from comment to answer).
Upvotes: 3
Reputation: 2314
RethinkDB primary key is for a single column, you can change it to any field instead of the default id
. When you call tableCreate
, you can pass a string for primaryKey
to use that column as primary key.
You cannot create primary key on multiple columns. Though, you can create secondary index on multi columns. However, RethinkDB doesn't have the unique index effect https://github.com/rethinkdb/rethinkdb/issues/1716.
If you want to do many to many relation ships, use 3 tables, you can do this:
Table T1: id, fields
Table T2: id, fields2
Pivot table P: t1_id, t2_id
Then you can use eqJoin
to join data, or concatMap/getAll
. Example:
r.table("P").eq_join("t1_id", r.table("T1"))
.zip()
.eq_join("t2_id", r.table("T2"))
.zip()
Basically from the pivot table, you join data with first table, then continue to join with second table.
You can read more about data table join https://www.rethinkdb.com/docs/table-joins/#many-to-many-relations in here.
Upvotes: 1