prisma / docs

📚 Prisma Documentation
https://www.prisma.io/docs
Apache License 2.0
976 stars 765 forks source link

Doc request(introspection): document opinionation about foreign key type validations #1608

Open tomhoule opened 3 years ago

tomhoule commented 3 years ago

At the database level, there can be foreign keys on columns that do not exactly match the referenced column's types. To illustrate this, in the database, the following situation can happen:

model Cat {
    name String @db.VarChar(200)
}

model Box {
    material BoxMaterial
    width Float
    height Float
    length Float
    catName String @db.VarChar(140)
    occupiedBy Cat @relation(fields: [catName], references: [name])
}

The important bit is that Box.catName is a VARCHAR(140), but Cat.name is a VARCHAR(200). The types could be even more different. The problem is that the PSL/the schema parser doesn't allow this, it validates that the types at both ends of the relation match. This makes a lot of sense for greenfield projects and avoids basic errors and bad schemas. But since it can happen in the database, when we introspect this, an invalid schema is produced.

We haven't done enough research to say whether any of the databases we support is strict in that regard.

Discussion: https://github.com/prisma/prisma/discussions/6700 Issue about having a tool to "autofix" the database schema in that scenario: https://github.com/prisma/prisma/issues/6205

tomhoule commented 3 years ago

How to fix this?

janpio commented 3 years ago

Migrate use case was e.g. here: https://github.com/prisma/prisma/discussions/6700 What I told the user there should optimally be available via docs.