5
Orionss
6y

Hello, can someone help me with this one ? I guess that the fucking SO elitist community would have beaten me to death if I asked this question.

I'm trying to create a relational table between a Tutorial object and a User object (to know which tutorial the user has access to) using Sequelize, and I figure out that I have two PRIMARY keys in my table. How is it possible ? UserID is also marked as Index.
The both keys are not Unique in themselves but their combinations are unique.

Comments
  • 1
    If a user can have access to multiple tutorials than you should create a tutorial access table. With a access number as an index. Followed by the tutorial id and the user id. Followed by dates and other info such as how long they have access for... Etc. Doing access in a table where userid is the index would mean that each user can only ever have 1 tutorial. Because there can only ever be 1 user row in that table.
  • 1
    If userid is the index your table probably looks like

    Tid 6 uid 1
    Tid 3 uid 2
    Tid 8 uid 3
    Tid 4 uid 4
    But if its a primary key and index you cant have multiple rows

    Im assumeing you want
    Tid 3 uid1
    Tid 5 uid1
    Tid 1 uid1
    Tid1 uid2

    If so you have to make a new table and remove the index and primary key set. And and a unique identity for each row. Like
    Rowid1 tid5 uid1
    Rowid2 tid3 uid 1
    Rowid3 tid1 uid1
    And make that your primary key and index.
  • 1
    @skprog That's what I would have done if I wasn't using an ORM (Sequelize for nodejs). But I'm using belongToMany association and it makes me this
  • 0
    @skprog Agree with u. But the last part with rowids is an anti pattern. Here you would use a composite primary key (2 attributes that make up a single key) of tid and uid. This avoids having duplicates, but does not limit the data you can save.
Add Comment