question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[DataModel] Compound Foreign Keys and Index Definitions

See original GitHub issue

The @@index definition currently has to refer to datamodel fields, but in the case of compound foreign keys two or more database fields are collapsed into one datamodel field. The indexes on the other hand can refer to the underlying database fields which are not in the datamodel (apart from being in the @map).

The @@unique definition theoretically has the same issue, but since we do not render it in the case of relations but instead make the relation 1:1 this issue does not surface.

Screen Shot 2020-02-14 at 11 05 40

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
mavileincommented, Mar 20, 2020

I would like to suggest one further simplification to the suggested syntax. To me the current syntax proposal feels very heavy for those reasons:

  • What’s the purpose of the name of the sub relation field when it does not exposed in the client? E.g. for @relation(fields: [sub_relation_field]) @map(["relation_column"]) the name seems to be irrelevant.
  • The syntax seems very dense. Name conflicts with other fields are very hard to spot.
  • With this approach we have 3 types of fields: scalar, relation and sub relation fields.

I would like to suggest an approach with which we could stick to the two types scalar and relation field. My suggestion is to turn relation fields into a completely virtual concept. Here are the rules i have in mind:

  • Every column of an underlying table gets mapped to a simple scalar field. Even those ones that are part of a foreign key and therefore represent a relation.
  • Relation Fields can be constructed by building upon those scalar fields. This way relation fields become very similar to foreign keys in SQL. A foreign key like FOREIGN KEY (a, b) REFERENCES OtherTable(col1, col2) is turned into relationField OtherTable @relation(fields: [a,b], references: [col1, col2].
  • Scalar fields that are used within a relation field become read only. (as already suggested by @sorenbs earlier)

Here’s a more detailed example:

SQL schema

CREATE TABLE post (
    id serial NOT NULL,
    author_first_name text NOT NULL,
    author_last_name text NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (author_first_name, author_last_name) REFERENCES user(first_name, last_name)
);

CREATE TABLE user (
    id serial NOT NULL,
    first_name text NOT NULL,
    last_name text NOT NULL,
    PRIMARY KEY (id),
    UNIQUE(first_name, last_name)
); 

corresponding Prisma schema

model Post {
    id Int @id
    authorFirstName String @map("author_first_name")
    authorLastName String @map("author_last_name")
    editorId Int @map("editor_id")

   // a relation field referencing the compound unique of `User`
    author User @relation(fields: [authorFirstName, authorLastName], references: [firstName, lastName])
   // a relation field referencing the id of `User`
    editor User @relation(fields: [editorId], references: [id])

  @@map("post")
}

model User {
    id Int @id
    firstName String @map("first_name")
    lastName String @map("last_name")

    @@unique([firstName, lastName])
    @@map("user")
}
0reactions
do4grcommented, Mar 27, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Foreign Keys | Enterprise Architect User Guide - Sparx Systems
A Foreign Key defines a column (or a collection of columns) that enforces a relationship between two Tables. It is the responsibility of...
Read more >
Defining Composite Primary and Foreign Keys - IBM
A composite key specifies multiple columns for a primary-key or foreign-key constraint. The next example creates two tables. The first table has a...
Read more >
Indexes on foreign keys - Ask TOM
I have read several books which have repeatedly mentioned creating indexes on foreign keys. I know one advantage is that it eliminates table-level...
Read more >
16.20 - Foreign Keys - Teradata Vantage NewSQL Engine
A foreign key is an attribute set in one relation based on an identical attribute set that acts as a primary or alternate...
Read more >
Glossary - ER/Studio Data Architect - Embarcadero DocWiki
A type of entity in which no foreign keys participate in its primary key. Index. A database object used to enforce unique values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found