Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Migration causes 'Duplicate key name' #148

Closed
stansm opened this issue Sep 25, 2019 · 7 comments
Closed

Migration causes 'Duplicate key name' #148

stansm opened this issue Sep 25, 2019 · 7 comments
Assignees
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug.
Milestone

Comments

@stansm
Copy link

stansm commented Sep 25, 2019

Prisma schema:

model User {
  id        String  @default(cuid()) @id
}
model Profile {
  id            String  @default(cuid()) @id
  user          User
  skills    Skill[]
}
model Skill {
  id            String  @default(cuid()) @id
  profiles    Profile[]
}

Modifying User model:

model User {
  id        String  @default(cuid()) @id
  someField String?
}

Causes unique index to be created again on the Skill model:

ALTER TABLE `User` ADD COLUMN `someField` varchar(191)   ;

CREATE UNIQUE INDEX `_ProfileToSkill_AB_unique` ON `_ProfileToSkill`(`A`,`B`)

Probably relates to #133

Prisma2 version: 2.0.0-alpha.205

@divyenduz
Copy link

divyenduz commented Sep 26, 2019

@stansm : I tried this with alpha Prisma2 version: 2.0.0-alpha.206 but I failed to reproduce, exact same steps. Can you please try again and see if you are able to repro?

Did you try this repro from scratch or did the DB had some data/migration info already?

What DB are you using? I tried it on SQLite and it seems to work.

@divyenduz divyenduz added bug/0-needs-info More information is needed for reproduction. kind/bug A reported bug. labels Sep 26, 2019
@divyenduz
Copy link

Able to reproduce this with MySQL

You can get the detailed db changes with prisma2 lift up --verbose
Or read about them in the ./migrations/MIGRATION_ID/README.md
Error: QueryError(QueryError(QueryError(MySqlError { ERROR 1061 (42000): Duplicate key name '_ProfileToSkill_AB_unique' }

stack backtrace:
   0: backtrace::backtrace::trace
   1: backtrace::capture::Backtrace::new_unresolved
   2: failure::backtrace::internal::InternalBacktrace::new
   3: <failure::backtrace::Backtrace as core::default::Default>::default
   4: prisma_query::connector::mysql::error::<impl core::convert::From<mysql::error::Error> for prisma_query::error::Error>::from
   5: prisma_query::connector::metrics::query
   6: <prisma_query::connector::mysql::Mysql as prisma_query::connector::queryable::Queryable>::query_raw
   7: <sql_migration_connector::migration_database::Mysql as sql_migration_connector::migration_database::MigrationDatabase>::query_raw
   8: sql_migration_connector::sql_database_step_applier::SqlDatabaseStepApplier::apply_next_step
   9: <sql_migration_connector::sql_database_step_applier::SqlDatabaseStepApplier as migration_connector::database_migration_step_applier::DatabaseMigrationStepApplier<sql_migration_connector::sql_migration::SqlMigration>>::apply_step
  10: <migration_connector::migration_applier::MigrationApplierImpl<T> as migration_connector::migration_applier::MigrationApplier<T>>::apply
  11: migration_engine::commands::apply_migration::ApplyMigrationCommand::handle_migration
  12: <migration_engine::commands::apply_migration::ApplyMigrationCommand as migration_engine::commands::command::MigrationCommand>::execute
  13: <migration_engine::api::MigrationApi<C,D> as migration_engine::api::GenericApi>::apply_migration
  14: migration_engine::api::rpc::RpcApi::create_sync_handler
  15: tokio_executor::enter::exit
  16: tokio_threadpool::blocking::blocking
  17: <futures::future::lazy::Lazy<F,R> as futures::future::Future>::poll
  18: futures::future::chain::Chain<A,B,C>::poll
  19: <futures::future::then::Then<A,B,F> as futures::future::Future>::poll
  20: <futures::future::lazy::Lazy<F,R> as futures::future::Future>::poll
  21: futures::future::chain::Chain<A,B,C>::poll
  22: <futures::future::then::Then<A,B,F> as futures::future::Future>::poll
  23: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  24: <futures::future::either::Either<A,B> as futures::future::Future>::poll
  25: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  26: <futures::future::map_err::MapErr<A,F> as futures::future::Future>::poll
  27: <futures::stream::and_then::AndThen<S,F,U> as futures::stream::Stream>::poll
  28: <futures::stream::forward::Forward<T,U> as futures::future::Future>::poll
  29: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  30: <futures::future::map_err::MapErr<A,F> as futures::future::Future>::poll
  31: futures::task_impl::std::set
  32: std::panicking::try::do_call
  33: __rust_maybe_catch_panic
  34: tokio_threadpool::task::Task::run
  35: tokio_threadpool::worker::Worker::run_task
  36: tokio_threadpool::worker::Worker::run
  37: std::thread::local::LocalKey<T>::with
  38: std::thread::local::LocalKey<T>::with
  39: tokio_reactor::with_default
  40: tokio::runtime::threadpool::builder::Builder::build::{{closure}}
  41: std::thread::local::LocalKey<T>::with
  42: std::thread::local::LocalKey<T>::with
  43: std::sys_common::backtrace::__rust_begin_short_backtrace
  44: std::panicking::try::do_call
  45: __rust_maybe_catch_panic
  46: core::ops::function::FnOnce::call_once{{vtable.shim}}
  47: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
  48: std::sys::unix::thread::Thread::new::thread_start
  49: _pthread_body
  50: _pthread_start
)

stack backtrace:
   0: backtrace::backtrace::trace
   1: backtrace::capture::Backtrace::new_unresolved
   2: failure::backtrace::internal::InternalBacktrace::new
   3: <failure::backtrace::Backtrace as core::default::Default>::default
   4: <sql_migration_connector::error::SqlError as core::convert::From<prisma_query::error::Error>>::from
   5: sql_migration_connector::sql_database_step_applier::SqlDatabaseStepApplier::apply_next_step
   6: <sql_migration_connector::sql_database_step_applier::SqlDatabaseStepApplier as migration_connector::database_migration_step_applier::DatabaseMigrationStepApplier<sql_migration_connector::sql_migration::SqlMigration>>::apply_step
   7: <migration_connector::migration_applier::MigrationApplierImpl<T> as migration_connector::migration_applier::MigrationApplier<T>>::apply
   8: migration_engine::commands::apply_migration::ApplyMigrationCommand::handle_migration
   9: <migration_engine::commands::apply_migration::ApplyMigrationCommand as migration_engine::commands::command::MigrationCommand>::execute
  10: <migration_engine::api::MigrationApi<C,D> as migration_engine::api::GenericApi>::apply_migration
  11: migration_engine::api::rpc::RpcApi::create_sync_handler
  12: tokio_executor::enter::exit
  13: tokio_threadpool::blocking::blocking
  14: <futures::future::lazy::Lazy<F,R> as futures::future::Future>::poll
  15: futures::future::chain::Chain<A,B,C>::poll
  16: <futures::future::then::Then<A,B,F> as futures::future::Future>::poll
  17: <futures::future::lazy::Lazy<F,R> as futures::future::Future>::poll
  18: futures::future::chain::Chain<A,B,C>::poll
  19: <futures::future::then::Then<A,B,F> as futures::future::Future>::poll
  20: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  21: <futures::future::either::Either<A,B> as futures::future::Future>::poll
  22: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  23: <futures::future::map_err::MapErr<A,F> as futures::future::Future>::poll
  24: <futures::stream::and_then::AndThen<S,F,U> as futures::stream::Stream>::poll
  25: <futures::stream::forward::Forward<T,U> as futures::future::Future>::poll
  26: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  27: <futures::future::map_err::MapErr<A,F> as futures::future::Future>::poll
  28: futures::task_impl::std::set
  29: std::panicking::try::do_call
  30: __rust_maybe_catch_panic
  31: tokio_threadpool::task::Task::run
  32: tokio_threadpool::worker::Worker::run_task
  33: tokio_threadpool::worker::Worker::run
  34: std::thread::local::LocalKey<T>::with
  35: std::thread::local::LocalKey<T>::with
  36: tokio_reactor::with_default
  37: tokio::runtime::threadpool::builder::Builder::build::{{closure}}
  38: std::thread::local::LocalKey<T>::with
  39: std::thread::local::LocalKey<T>::with
  40: std::sys_common::backtrace::__rust_begin_short_backtrace
  41: std::panicking::try::do_call
  42: __rust_maybe_catch_panic
  43: core::ops::function::FnOnce::call_once{{vtable.shim}}
  44: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
  45: std::sys::unix::thread::Thread::new::thread_start
  46: _pthread_body
  47: _pthread_start
)

stack backtrace:
   0: backtrace::backtrace::trace
   1: backtrace::capture::Backtrace::new_unresolved
   2: failure::backtrace::internal::InternalBacktrace::new
   3: <failure::backtrace::Backtrace as core::default::Default>::default
   4: sql_migration_connector::error::<impl core::convert::From<sql_migration_connector::error::SqlError> for migration_connector::error::ConnectorError>::from
   5: <sql_migration_connector::sql_database_step_applier::SqlDatabaseStepApplier as migration_connector::database_migration_step_applier::DatabaseMigrationStepApplier<sql_migration_connector::sql_migration::SqlMigration>>::apply_step
   6: <migration_connector::migration_applier::MigrationApplierImpl<T> as migration_connector::migration_applier::MigrationApplier<T>>::apply
   7: migration_engine::commands::apply_migration::ApplyMigrationCommand::handle_migration
   8: <migration_engine::commands::apply_migration::ApplyMigrationCommand as migration_engine::commands::command::MigrationCommand>::execute
   9: <migration_engine::api::MigrationApi<C,D> as migration_engine::api::GenericApi>::apply_migration
  10: migration_engine::api::rpc::RpcApi::create_sync_handler
  11: tokio_executor::enter::exit
  12: tokio_threadpool::blocking::blocking
  13: <futures::future::lazy::Lazy<F,R> as futures::future::Future>::poll
  14: futures::future::chain::Chain<A,B,C>::poll
  15: <futures::future::then::Then<A,B,F> as futures::future::Future>::poll
  16: <futures::future::lazy::Lazy<F,R> as futures::future::Future>::poll
  17: futures::future::chain::Chain<A,B,C>::poll
  18: <futures::future::then::Then<A,B,F> as futures::future::Future>::poll
  19: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  20: <futures::future::either::Either<A,B> as futures::future::Future>::poll
  21: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  22: <futures::future::map_err::MapErr<A,F> as futures::future::Future>::poll
  23: <futures::stream::and_then::AndThen<S,F,U> as futures::stream::Stream>::poll
  24: <futures::stream::forward::Forward<T,U> as futures::future::Future>::poll
  25: <futures::future::map::Map<A,F> as futures::future::Future>::poll
  26: <futures::future::map_err::MapErr<A,F> as futures::future::Future>::poll
  27: futures::task_impl::std::set
  28: std::panicking::try::do_call
  29: __rust_maybe_catch_panic
  30: tokio_threadpool::task::Task::run
  31: tokio_threadpool::worker::Worker::run_task
  32: tokio_threadpool::worker::Worker::run
  33: std::thread::local::LocalKey<T>::with
  34: std::thread::local::LocalKey<T>::with
  35: tokio_reactor::with_default
  36: tokio::runtime::threadpool::builder::Builder::build::{{closure}}
  37: std::thread::local::LocalKey<T>::with
  38: std::thread::local::LocalKey<T>::with
  39: std::sys_common::backtrace::__rust_begin_short_backtrace
  40: std::panicking::try::do_call
  41: __rust_maybe_catch_panic
  42: core::ops::function::FnOnce::call_once{{vtable.shim}}
  43: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
  44: std::sys::unix::thread::Thread::new::thread_start
  45: _pthread_body
  46: _pthread_start

@divyenduz divyenduz added bug/2-confirmed We have confirmed that this is a bug. process/candidate Candidate for next Milestone. and removed bug/0-needs-info More information is needed for reproduction. labels Sep 26, 2019
@janpio janpio added this to the Preview 14 milestone Sep 27, 2019
@janpio janpio removed the process/candidate Candidate for next Milestone. label Sep 27, 2019
tomhoule added a commit to prisma/prisma-engines that referenced this issue Sep 30, 2019
Issue: prisma/migrate#148

This does not resolve all ambiguities. A more general solution would
involve including the index name in the inferred Index struct.
tomhoule added a commit to prisma/prisma-engines that referenced this issue Sep 30, 2019
Issue: prisma/migrate#148

This does not resolve all ambiguities. A more general solution would
involve including the index name in the introspected Index struct.
tomhoule added a commit to prisma/prisma-engines that referenced this issue Sep 30, 2019
Issue: prisma/migrate#148

This does not resolve all ambiguities. A more general solution would
involve including the index name in the introspected Index struct.
tomhoule added a commit to prisma/prisma-engines that referenced this issue Sep 30, 2019
Issue: prisma/migrate#148

This does not resolve all ambiguities. A more general solution would
involve including the index name in the introspected Index struct.
tomhoule added a commit to prisma/prisma-engines that referenced this issue Oct 1, 2019
Issue: prisma/migrate#148

This does not resolve all ambiguities. A more general solution would
involve including the index name in the introspected Index struct.
@tomhoule
Copy link
Contributor

tomhoule commented Oct 1, 2019

This should be fixed by prisma/prisma-engines#80

It was an issue with how we read indexes from MySQL that caused the migration engine to try to create indexes that already existed. The fix will be in the next preview :)

@tomhoule tomhoule closed this as completed Oct 1, 2019
@divyenduz
Copy link

Reopening while this is not in alpha 🙏

@divyenduz divyenduz reopened this Oct 2, 2019
@mavilein
Copy link

A fix for this was merged to alpha 9 days ago. It will be released as a normal preview tomorrow.
Forgot to close this.

@quentinuys

This comment has been minimized.

@pantharshit00

This comment has been minimized.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests

7 participants