[Quick 5] Can Quick Handle a Relationship that uses Composite (Multiple) Keys?

In most databases, relationships are defined with a primary key, PK, in table A and a foreign key, FK, in table B. However, some databases use multiple columns as the PK, which is known as a composite key.

Is it possible within Quick to establish relationships, like HasMany, if the tables use composite keys?

return hasMany( "Post", "FK_userID", "relatedPostId" ); // <-- singular PK/FK example

It might not be documented the best, but you can use composite or compound keys in both primary keys and relationships in Quick.

And each relationship method shows that it can take an array of keys as the foreign or related keys. It will also use multiple if the primary key of the related entity is a compound key.

For example: https://quick.ortusbooks.com/guide/relationships/relationship-types/belongsto#signature

1 Like

This is great! Thanks @elpete. I submitted a change request to the docs to help explain composite relationships. Feel free to edit/change as you see fit. :slight_smile:

@elpete, I have one follow up question:
In the docs, it says you can define a primary key in an entity by using:

variables._key = "custom_id"; // custom primary key other than 'id'

Does this also mean you can use an array of properties if the entity uses a composite key? So, in other words, you could define it like this:

variables._key = [ "countryCode", "number" ]; // composite key columns

Let me know, and I will update the docs accordingly. :slight_smile:

That’s correct.

(And now I’m adding more characters since I need at least 20, lol)

1 Like