Skip to content
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

Schema Validation fails with panic thread 'main' panicked at 'Expected ID field ... to be present on the model' #1681

Closed
janpio opened this issue Feb 21, 2020 · 6 comments
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: AUMFIDARR topic: prisma generate CLI: prisma generate topic: test-utils
Milestone

Comments

@janpio
Copy link
Member

janpio commented Feb 21, 2020

Many database schemas that used to validate with 0 errors, now crash with a Rust Panic similar to this one:

Error: Schema parsing
thread 'main' panicked at 'Expected ID field rule_id to be present on the model', src/libcore/option.rs:1188:5
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:84
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:61
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1025
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1426
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:193
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:210
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:471
  11: rust_begin_unwind
             at src/libstd/panicking.rs:375
  12: core::panicking::panic_fmt
             at src/libcore/panicking.rs:84
  13: core::option::expect_failed
             at src/libcore/option.rs:1188
  14: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
  15: prisma_models::fields::Fields::find_multipart_id
  16: once_cell::imp::OnceCell<T>::initialize::{{closure}}
  17: once_cell::imp::initialize_inner
  18: once_cell::imp::OnceCell<T>::initialize
  19: prisma_models::fields::Fields::id
  20: query_core::schema_builder::input_type_builder::InputTypeBuilderBase::where_unique_object_type
  21: query_core::schema_builder::object_type_builder::ObjectTypeBuilder::many_records_arguments
  22: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
  23: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
  24: query_core::schema_builder::object_type_builder::ObjectTypeBuilder::new
  25: query_core::schema_builder::query_schema_builder::QuerySchemaBuilder::new
  26: prisma::cli::CliCommand::dmmf
  27: prisma::main::{{closure}}
  28: <std::future::GenFuture<T> as core::future::future::Future>::poll
  29: tokio::runtime::enter::Enter::block_on
  30: tokio::runtime::thread_pool::ThreadPool::block_on
  31: tokio::runtime::context::enter
  32: tokio::runtime::handle::Handle::enter
  33: prisma::main
  34: std::rt::lang_start::{{closure}}
  35: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  36: std::panicking::try::do_call
             at src/libstd/panicking.rs:292
  37: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:78
  38: std::panicking::try
             at src/libstd/panicking.rs:270
  39: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  40: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  41: main
  42: __libc_start_main
  43: <unknown>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

(Internal Link: https://github.com/prisma/introspection-engine-output/commit/2356683bf674ef25defe7d1918e3b4f4c9a31735)

image

(Update: 90 schemas are affected)

@janpio janpio added topic: prisma generate CLI: prisma generate bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. labels Feb 21, 2020
@janpio janpio changed the title Schema Validation fails with panic for alpha.820: thread 'main' panicked at 'Expected ID field name1 to be present on the model' Schema Validation fails with panic for alpha.820: thread 'main' panicked at 'Expected ID field ... to be present on the model' Feb 21, 2020
@janpio janpio changed the title Schema Validation fails with panic for alpha.820: thread 'main' panicked at 'Expected ID field ... to be present on the model' alpha.820: Schema Validation fails with panic thread 'main' panicked at 'Expected ID field ... to be present on the model' Feb 21, 2020
@janpio janpio changed the title alpha.820: Schema Validation fails with panic thread 'main' panicked at 'Expected ID field ... to be present on the model' alpha.820: Schema Validation fails with panic thread 'main' panicked at 'Expected ID field ... to be present on the model' Feb 21, 2020
@janpio
Copy link
Member Author

janpio commented Feb 21, 2020

This for example happens for this SQLite file: https://github.com/prisma/introspection-engine-output/blob/master/sqlite_dbs/chinook.db

λ prisma2 validate
Error: Schema parsing
thread 'main' panicked at 'Expected ID field PlaylistId to be present on the model', src\libcore\option.rs:1188:5stack backtrace:
   0: backtrace::backtrace::dbghelp::trace
             at C:\Users\VssAdministrator\.cargo\registry\src\github.com-1ecc6299db9ec823\backtrace-0.3.40\src\backtrace/dbghelp.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at C:\Users\VssAdministrator\.cargo\registry\src\github.com-1ecc6299db9ec823\backtrace-0.3.40\src\backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src\libstd\sys_common/backtrace.rs:84
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src\libstd\sys_common/backtrace.rs:61
   4: core::fmt::write
             at src\libcore\fmt/mod.rs:1025
   5: std::io::Write::write_fmt
             at src\libstd\io/mod.rs:1426
   6: std::sys_common::backtrace::_print
             at src\libstd\sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src\libstd\sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src\libstd/panicking.rs:193
   9: std::panicking::default_hook
             at src\libstd/panicking.rs:210
  10: std::panicking::rust_panic_with_hook
             at src\libstd/panicking.rs:471
  11: rust_begin_unwind
             at src\libstd/panicking.rs:375
  12: core::panicking::panic_fmt
             at src\libcore/panicking.rs:84
  13: core::option::expect_failed
             at src\libcore/option.rs:1188
  14: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
  15: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
  16: prisma_models::fields::Fields::find_multipart_id
  17: once_cell::imp::OnceCell<T>::initialize::{{closure}}
  18: once_cell::imp::initialize_inner
  19: once_cell::imp::OnceCell<T>::initialize
  20: prisma_models::fields::Fields::id
  21: query_core::schema_builder::input_type_builder::InputTypeBuilderBase::where_unique_object_type
  22: query_core::schema_builder::object_type_builder::ObjectTypeBuilder::many_records_arguments
  23: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
  24: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
  25: query_core::schema_builder::object_type_builder::ObjectTypeBuilder::new
  26: query_core::schema_builder::query_schema_builder::QuerySchemaBuilder::new
  27: prisma::cli::CliCommand::dmmf
  28: prisma::main::{{closure}}
  29: <std::future::GenFuture<T> as core::future::future::Future>::poll
  30: tokio::runtime::enter::Enter::block_on
  31: tokio::runtime::thread_pool::ThreadPool::block_on
  32: tokio::runtime::context::enter
  33: tokio::runtime::handle::Handle::enter
  34: prisma::main
  35: std::rt::lang_start::{{closure}}
  36: std::rt::lang_start_internal::{{closure}}
             at src\libstd/rt.rs:52
  37: std::panicking::try::do_call
             at src\libstd/panicking.rs:292
  38: __rust_maybe_catch_panic
             at src\libpanic_unwind/lib.rs:78
  39: std::panicking::try
             at src\libstd/panicking.rs:270
  40: std::panic::catch_unwind
             at src\libstd/panic.rs:394
  41: std::rt::lang_start_internal
             at src\libstd/rt.rs:51
  42: main
  43: _tmainCRTStartup
  44: mainCRTStartup
  45: sqlite3GenerateConstraintChecks
  46: sqlite3GenerateConstraintChecks
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The model looks like this:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:chinook.db"
}

model albums {
  AlbumId  Int      @default(autoincrement()) @id
  Title    String
  ArtistId artists
  tracks   tracks[]

  @@index([ArtistId], name: "IFK_AlbumArtistId")
}

model artists {
  ArtistId Int      @default(autoincrement()) @id
  Name     String?
  albums   albums[]
}

model customers {
  Address      String?
  City         String?
  Company      String?
  Country      String?
  CustomerId   Int        @default(autoincrement()) @id
  Email        String
  Fax          String?
  FirstName    String
  LastName     String
  Phone        String?
  PostalCode   String?
  State        String?
  SupportRepId employees?
  invoices     invoices[]

  @@index([SupportRepId], name: "IFK_CustomerSupportRepId")
}

model employees {
  Address    String?
  BirthDate  DateTime?
  City       String?
  Country    String?
  Email      String?
  EmployeeId Int         @default(autoincrement()) @id
  Fax        String?
  FirstName  String
  HireDate   DateTime?
  LastName   String
  Phone      String?
  PostalCode String?
  State      String?
  Title      String?
  ReportsTo  employees?  @relation("employeesToemployees_ReportsTo")
  customers  customers[]
  employees  employees[] @relation("employeesToemployees_ReportsTo")

  @@index([ReportsTo], name: "IFK_EmployeeReportsTo")
}

model genres {
  GenreId Int      @default(autoincrement()) @id
  Name    String?
  tracks  tracks[]
}

model invoices {
  BillingAddress    String?
  BillingCity       String?
  BillingCountry    String?
  BillingPostalCode String?
  BillingState      String?
  InvoiceDate       DateTime
  InvoiceId         Int             @default(autoincrement()) @id
  Total             Float
  CustomerId        customers
  invoice_items     invoice_items[]

  @@index([CustomerId], name: "IFK_InvoiceCustomerId")
}

model invoice_items {
  InvoiceLineId Int      @default(autoincrement()) @id
  Quantity      Int
  UnitPrice     Float
  InvoiceId     invoices
  TrackId       tracks

  @@index([TrackId], name: "IFK_InvoiceLineTrackId")
  @@index([InvoiceId], name: "IFK_InvoiceLineInvoiceId")
}

model media_types {
  MediaTypeId Int      @default(autoincrement()) @id
  Name        String?
  tracks      tracks[]
}

model playlists {
  Name           String?
  PlaylistId     Int              @default(autoincrement()) @id
  playlist_track playlist_track[]
}

model playlist_track {
  PlaylistId playlists
  TrackId    tracks

  @@id([PlaylistId, TrackId])
  @@index([TrackId], name: "IFK_PlaylistTrackTrackId")
}

model tracks {
  Bytes          Int?
  Composer       String?
  Milliseconds   Int
  Name           String
  TrackId        Int              @default(autoincrement()) @id
  UnitPrice      Float
  AlbumId        albums?
  GenreId        genres?
  MediaTypeId    media_types
  invoice_items  invoice_items[]
  playlist_track playlist_track[]

  @@index([MediaTypeId], name: "IFK_TrackMediaTypeId")
  @@index([GenreId], name: "IFK_TrackGenreId")
  @@index([AlbumId], name: "IFK_TrackAlbumId")
}

@janpio
Copy link
Member Author

janpio commented Feb 21, 2020

A similar panic is reproducible with this smaller schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:chinook.db"
}

model a {
  aId String
  bId  b

  @@id([aId, bId])
}

model b {
  bId  Int  @default(autoincrement()) @id
  a a[]
}
λ prisma2 validate
Error: Schema parsing
thread 'main' panicked at 'Expected ID field bId to be present on the model', src\libcore\option.rs:1188:5
stack backtrace:
   0: backtrace::backtrace::dbghelp::trace
             at C:\Users\VssAdministrator\.cargo\registry\src\github.com-1ecc6299db9ec823\backtrace-0.3.40\src\backtrace/dbghelp.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at C:\Users\VssAdministrator\.cargo\registry\src\github.com-1ecc6299db9ec823\backtrace-0.3.40\src\backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src\libstd\sys_common/backtrace.rs:84
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src\libstd\sys_common/backtrace.rs:61
   4: core::fmt::write
             at src\libcore\fmt/mod.rs:1025
   5: std::io::Write::write_fmt
             at src\libstd\io/mod.rs:1426
   6: std::sys_common::backtrace::_print
             at src\libstd\sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src\libstd\sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src\libstd/panicking.rs:193
   9: std::panicking::default_hook
             at src\libstd/panicking.rs:210
  10: std::panicking::rust_panic_with_hook
             at src\libstd/panicking.rs:471
  11: rust_begin_unwind
             at src\libstd/panicking.rs:375
  12: core::panicking::panic_fmt
             at src\libcore/panicking.rs:84
  13: core::option::expect_failed
             at src\libcore/option.rs:1188
  14: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
  15: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
  16: prisma_models::fields::Fields::find_multipart_id
  17: once_cell::imp::OnceCell<T>::initialize::{{closure}}
  18: once_cell::imp::initialize_inner
  19: once_cell::imp::OnceCell<T>::initialize
  20: prisma_models::fields::Fields::id
  21: query_core::schema_builder::input_type_builder::InputTypeBuilderBase::where_unique_object_type
  22: query_core::schema_builder::object_type_builder::ObjectTypeBuilder::many_records_arguments
  23: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &mut F>::call_once
  24: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
  25: query_core::schema_builder::object_type_builder::ObjectTypeBuilder::new
  26: query_core::schema_builder::query_schema_builder::QuerySchemaBuilder::new
  27: prisma::cli::CliCommand::dmmf
  28: prisma::main::{{closure}}
  29: <std::future::GenFuture<T> as core::future::future::Future>::poll
  30: tokio::runtime::enter::Enter::block_on
  31: tokio::runtime::thread_pool::ThreadPool::block_on
  32: tokio::runtime::context::enter
  33: tokio::runtime::handle::Handle::enter
  34: prisma::main
  35: std::rt::lang_start::{{closure}}
  36: std::rt::lang_start_internal::{{closure}}
             at src\libstd/rt.rs:52
  37: std::panicking::try::do_call
             at src\libstd/panicking.rs:292
  38: __rust_maybe_catch_panic
             at src\libpanic_unwind/lib.rs:78
  39: std::panicking::try
             at src\libstd/panicking.rs:270
  40: std::panic::catch_unwind
             at src\libstd/panic.rs:394
  41: std::rt::lang_start_internal
             at src\libstd/rt.rs:51
  42: main
  43: _tmainCRTStartup
  44: mainCRTStartup
  45: sqlite3GenerateConstraintChecks
  46: sqlite3GenerateConstraintChecks
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Feb 24, 2020
@janpio
Copy link
Member Author

janpio commented Feb 24, 2020

Problem

Query Engine tries to create the "Input Types" by looking at @@id. It finds aId because that is a scalar fields, but does not find bId because that is not a scalar field but part of a relation. Then it panics and gives up.

Decision

Allow @@id to mention non scalar fields.

Temporary Guardrail

Drop the relation on the (currently) non scalar field to make it a scalar field, that is supported already. Write a comment "next" to the field to communicate to the user what happened.

@divyenduz
Copy link
Contributor

Test Utils DBs affected by this:

MySQL

PG:

@janpio janpio changed the title alpha.820: Schema Validation fails with panic thread 'main' panicked at 'Expected ID field ... to be present on the model' Schema Validation fails with panic thread 'main' panicked at 'Expected ID field ... to be present on the model' Mar 6, 2020
@divyenduz divyenduz modified the milestones: Preview 24 Old, Preview 25 Mar 13, 2020
@janpio
Copy link
Member Author

janpio commented Mar 18, 2020

The general bug is fixed here with the recent merges and active with the merge of prisma/prisma-engines#578.

(For now it is unclear how the test-utils links here actually were relevant, @divyenduz will look into those.)

@janpio janpio closed this as completed Mar 18, 2020
@divyenduz
Copy link
Contributor

divyenduz commented Mar 19, 2020

@janpio test-utils failed at generate regarding this, as you can see, all the logs above point to a generate log file.

tl;dr; the relation to test-utils here is only that we found this out when trying test-utils out and mass generating. Query generator would have shown same results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: AUMFIDARR topic: prisma generate CLI: prisma generate topic: test-utils
Projects
None yet
Development

No branches or pull requests

6 participants