Skip to content

[Native Types] Ensure that fields of unsupported types aren't dropped #3673

Closed

Description

@thebiglabasky

Problem

It is difficult to maintain the schema and using the client for applications dependent on types that are unsupported by Prisma.
More specifically, whenever introspecting the database, columns of unsupported types are commented out. In these situations, using migrate to update the database schema again later will trigger the deletion of the column which isn't wanted.

Suggested solution

Have a way to keep columns of unsupported types around in the database, even if not having these usable within the Prisma client.

Possibilities:

model Bla {
  myField Unsupported("MACADDR")
}
// Or
model Bla {
  myField Unsupported @Unsupported("MACADDR")
}

Additional context

Using $queryRaw on unsupported field types may trigger errors depending on the type as the Rust engine may not be able to deserialize it properly. I assume that going into that would probably mean to look into supporting these field types as I suspect that could be equivalent to actually supporting them.

Activity

  1. do4gr commented on Dec 30, 2020

    @do4gr
    Member

    Problem

    Currently, PSL does not support certain database schema components:

    • not all native types available (not even after the native types initiative goes live). Examples: PostGIS geolocation
    • models that are not valid according to certain Prisma rules, Example: models with no unique column on the table

    Behaviour when using first Introspection then Migrate:

    Because there is no way to represent these models/fields, introspection will put these behind comments. These comments are merely informative for the user. Since the schema parser will ignore these, they are also ignored by Prisma Migrate, leading to Prisma Migrate trying to drop the fields and columns that are not supported

    Behaviour when using Migrate standalone, no introspection:

    Users creating native types which are unsupported manually in their migrations, e.g. adding geolocation columns in their SQL migrations, also deal with some undesired effects:

    • the columns will not be reflected in the Prisma schema
    • Migrate will try to remove these columns the next time they generate a migration

    Behaviour when using Query Engine / Client

    The query engine and the client it generates currently have no knowledge of commented out columns. This means when generating the API they do not take into consideration whether there are unsupported required fields on the table. This leads to them exposing an API that contains create operations that will always fail at runtime if there is no db level default for this columns.

    Solution

    Introducing the Unsupported type

    We solve the current problems with a 2 part solution. First we will introduce a new Unsupported type in lieu of commented out types in the Prisma to persist information about not officially supported types during re-parsing of the schema.

    model Event {
    	id         Int @id
           //Prisma currently does not support this type: `Geolocation` 
           location   Unsupported("Geolocation")  @unique @default(degenerated())
    }

    This allows our engines to retain knowledge of the existence of this column even if they are not fully able to handle it. This means:

    • the migration engine will no longer try to drop it when migrating
    • the query engine will know when unsupported columns make create operations impossible due to being required and missing defaults

    Introducing the @@ignore and @ignore

    A second source of commented out entities are tables that do not contain unique identifiers. This can for example be the case for join tables used in relations. Currently these need to be commented out since the query engine cannot handle them and would therefore subsequently be dropped by migrate.

    The introduction of @@ignore and @ignore allows us to split missing support in the query engine from support in the migration engine. Models that the query engine cannot handle will be automatically decorated with @@ignore during introspection and not be exposed to the query engine. Migrate will still be able to operate on these and could for example be used to add a unique property. After the manual removal of the @@unique afterwards, the query engine could then operate on these.

    model NoUniqueManyRelation {
    	user       Int @relation("UserRelation"....)
    	event      Int @relation("EventRelation"....)
    
    	@@ignore
    }

    The @ignore is the field level pendant to this.

    We see two main uses cases for these: The first one is to address our current problem of migrate dropping unsupported entities if we merely comment them out. This is shown in the example above. The second use case would be developers not wanting to expose certain parts of their schema in the generated client API. In this case they could manually annotate models or fields with these directives.

    Implementation

    • Schema level: support for Unsupported and dbgenerated(). This includes parsing, rendering and necessary validations. Introspection uses this feature instead of commenting out entities.
    • Schema level: support for @@ignore. This includes parsing, rendering and necessary validations. Introspection uses this feature instead of commenting out entities.
    • Schema level: support for @ignore. This includes parsing, rendering and necessary validations. Introspection uses this feature instead of commenting out entities.
    • Re-Introspection: support for the unsupported type
    • Re-Introspection: support for the @@ignore directive
    • Re-Introspection: support for the @ignore directive
    • Query Engine: Adjust the internal data model generation to exclude these entities from API generation
    • Query Engine: do not expose create operations for models with required Unsupported fields without defaults
    • Migrate: add limited diffing and migrate capabilities for Unsupported columns
    • Docs: documentation and examples for these new properties
  2. added this to the 2.16.0 milestone on Jan 21, 2021
  3. albertoperdomo commented on Feb 3, 2021

    @albertoperdomo
    Contributor

    Unsupported is live, keeping this open until @ignore and @@ignore are fully implemented.

  4. modified the milestones: 2.16.0, 2.17.0 on Feb 3, 2021
  5. removed their assignment
    on Feb 3, 2021
  6. do4gr commented on Feb 12, 2021

    @do4gr
    Member

    This is now implemented apart from the docs part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions