Skip to content

Introspection: MongoDB: Warnings mention Native Types instead of PSL types (e.g. Document instead of Json) #12321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jolg42 opened this issue Mar 14, 2022 · 4 comments · Fixed by prisma/prisma-engines#2772
Assignees
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/improvement An improvement to existing feature and code. topic: introspection topic: mongodb topic: native database types
Milestone

Comments

@Jolg42
Copy link
Contributor

Jolg42 commented Mar 14, 2022

Now that we use Json for properties with different types, we might want to tweak the language used in the comment and CLI warning:

Example 1, (since Json is picked as default with engine PR)

      // *** WARNING ***
      // 
      // The following fields had data stored in multiple types. Either use Json or normalize data to the wanted type.
      // - Model "users", field: "numberOrString1", chosen data type: "Document"
      // - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Document"
      // - Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Document"

Here for the field field: "numberOrString3" the chosen data type is Document but the type in the schema.prisma file will be Json

See db pull test

test('introspection --print --composite-type-depth=-1 (no existing models)', async () => {
ctx.fixture('schema-only-mongodb')
const introspect = new DbPull()
const result = introspect.parse(['--schema=./prisma/no-model.prisma', '--print', '--composite-type-depth=-1'])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchInlineSnapshot(`
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
datasource my_db {
provider = "mongodb"
url = env("TEST_MONGO_URI_MIGRATE")
}
type UsersHobbies {
name String
/// Multiple data types found: String: 50%, Int32: 50% out of 2 sampled entries
numberOrString2 Int?
objects UsersHobbiesObjects[]
tags String[]
}
type UsersHobbiesObjects {
name String
/// Multiple data types found: String: 50%, Int32: 50% out of 2 sampled entries
numberOrString3 Int
tags String[]
}
model users {
id String @id @default(auto()) @map("_id") @my_db.ObjectId
admin Boolean
email String
hobbies UsersHobbies[]
name String
/// Multiple data types found: String: 50%, Int32: 50% out of 2 sampled entries
numberOrString1 Int
}
// introspectionSchemaVersion: NonPrisma,
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n')).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n')).toMatchInlineSnapshot(`
// *** WARNING ***
//
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "users", field: "numberOrString1", chosen data type: "Int32"
// - Type "UsersHobbies", field: "numberOrString2", chosen data type: "Int32"
// - Type "UsersHobbiesObjects", field: "numberOrString3", chosen data type: "Int32"
//
`)
})

Example 2 (Before the default changed to Json)

// *** WARNING ***
// 
// The following fields had data stored in multiple types. The most common type was chosen. If loading data with a type that does not match the one in the data model, the client will crash. Please see the issue: https://github.com/prisma/prisma/issues/9654
// - Model "shipwrecks", field: "depth", chosen data type: "Int32"
// 

Here for the field field: "depth" the chosen data type is Int32 but the type in the schema.prisma file will be Int

See https://github.com/prisma/introspection-ci/blob/4c4b4f7aeaa5d0eea96d4a3d00630c8a52b3e3f9/introspection-analysis/output/mongodb_public/sample_geospatial_stderr.log

Internal thread https://prisma-company.slack.com/archives/C4GCG53BP/p1647246722174479

MongoDB data

[
  {
    "name": "Jane Smith",
    "email": "1@prisma.io",
    "admin": true,
    "numberOrString1": 1234,
    "hobbies": [
      {
        "name": "Swimming",
        "tags": ["tag"],
        "numberOrString2": 1234,
        "objects": [
          {
            "name": "Object 1",
            "tags": ["tag"],
            "numberOrString3": 1234
          }
        ]
      }
    ]
  },
  {
    "name": "John Smith",
    "email": "2@prisma.io",
    "admin": false,
    "numberOrString1": "567",
    "hobbies": [
      {
        "name": "Running",
        "tags": ["tag"],
        "numberOrString2": "567",
        "objects": [
          {
            "name": "Object 1",
            "tags": ["tag"],
            "numberOrString3": "567"
          }
        ]
      },
      {
        "name": "Coding",
        "tags": ["tag"]
      }
    ]
  }
]

Note: introspection-ci results when default changed to Json https://github.com/prisma/introspection-ci/commit/b362f3e9ab0361a9b9efa96c382359303273cc4d

@Jolg42 Jolg42 added kind/improvement An improvement to existing feature and code. topic: introspection topic: native database types domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. topic: mongodb labels Mar 14, 2022
@Jolg42 Jolg42 changed the title Introspection: MongoDB: Warnings mention Native Types instead of PSL types (e.g. Int32 instead of Int) Introspection: MongoDB: Warnings mention Native Types instead of PSL types (e.g. Document instead of Json) Mar 15, 2022
@janpio
Copy link
Contributor

janpio commented Mar 15, 2022

We can probably remove the "chosen data type" information from the CLI warning completely. The introduction of the list can mention that already and can be improved to be more readable. (We can also create documentation how to proceed from there to fix the data and be able to change the schema to one of the existing data types.)

Could look like this:

      // *** WARNING ***
      // 
      // The following fields had data stored in multiple types and were written as `Json` to the schema, and a comment added with type information. 
      // Go to https://pris.ly/d/mongodb-field-multiple-types to learn how to proceed from here.
      // - Model "users", field: "numberOrString1"
      // - Type "UsersHobbies", field: "numberOrString2"
      // - Type "UsersHobbiesObjects", field: "numberOrString3"

@Jolg42
Copy link
Contributor Author

Jolg42 commented Mar 16, 2022

That would be very easy to change

@janpio
Copy link
Contributor

janpio commented Mar 16, 2022

I think that would fix the concrete problem in the CLI.
It would still leave the MongoDB types in the comment we render to the schema, but we should track that separately in a new issue (and also work on it, but independently)

@janpio
Copy link
Contributor

janpio commented Mar 21, 2022

Ok, seems we went with changing the actual returned data type string instead:

// The following fields had data stored in multiple types. Either use Json or normalize data to the wanted type.
- // - Type "MoviesImdb", field: "rating", chosen data type: "Document"
- // - Type "MoviesImdb", field: "votes", chosen data type: "Document"
+ // - Type "MoviesImdb", field: "rating", chosen data type: "Json"
+ // - Type "MoviesImdb", field: "votes", chosen data type: "Json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/improvement An improvement to existing feature and code. topic: introspection topic: mongodb topic: native database types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants