Test that an existing unique constraint can be migrated to PK
@@ -2353,3 +2353,58 @@ async fn models_with_an_autoincrement_field_as_part_of_a_multi_field_id_can_be_c
Ok(())
}
+
+#[test_each_connector]
+async fn migrating_a_unique_constraint_to_a_primary_key_works(api: &TestApi) -> TestResult {
+ let dm = r#"
+ model model1 {
+ id String @default(cuid()) @id
+ a String
+ b String
+ c String
+
+ @@unique([a, b, c])
+
+ }
+ "#;
+
+ api.schema_push(dm).send().await?.assert_green()?;
+
+ api.assert_schema().await?.assert_table("model1", |table| {
+ table
+ .assert_pk(|pk| pk.assert_columns(&["id"]))?
+ .assert_index_on_columns(&["a", "b", "c"], |idx| idx.assert_is_unique())
+ })?;
+
+ api.insert("model1")
+ .value("id", "the-id")
+ .value("a", "the-a")
+ .value("b", "the-b")
+ .value("c", "the-c")
+ .result_raw()
+ .await?;
+
+ let dm2 = r#"
+ model model1 {
+ a String
+ b String
+ c String
+
+ @@id([a, b, c])
+
+ }
+ "#;
+
+ api.schema_push(dm2)
+ .force(true)
+ .send()
+ .await?
+ .assert_executable()?
+ .assert_warnings(&["The migration will change the primary key for the `model1` table. If it partially fails, the table could be left without primary key constraint.".into(), "You are about to drop the column `id` on the `model1` table, which still contains 1 non-null values.".into()])?;
+
+ api.assert_schema().await?.assert_table("model1", |table| {
+ table.assert_pk(|pk| pk.assert_columns(&["a", "b", "c"]))
+ })?;
+
+ Ok(())
+}
