From 55fed903abbca3bb3c96dba3d4101a5025ed8798 Mon Sep 17 00:00:00 2001
From: Kamil Mielnik <kamil@kamilmielnik.com>
Date: Thu, 27 Jun 2024 19:09:02 +0700
Subject: [PATCH] Repro #41785 - Using Column picker to remove columns on model
 breaks question with ERROR: column source.name_2 does not exist Position
 (#44808)

* Add repro for #41785

* Update comment
---
 .../helpers/e2e-ad-hoc-question-helpers.js    |  8 +++
 .../scenarios/models/reproductions.cy.spec.ts | 65 +++++++++++++++++--
 2 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/e2e/support/helpers/e2e-ad-hoc-question-helpers.js b/e2e/support/helpers/e2e-ad-hoc-question-helpers.js
index 06d66308322..a5cfd92fb20 100644
--- a/e2e/support/helpers/e2e-ad-hoc-question-helpers.js
+++ b/e2e/support/helpers/e2e-ad-hoc-question-helpers.js
@@ -40,6 +40,14 @@ export function startNewQuestion() {
   cy.visit(`/question/notebook#${hash}`);
 }
 
+/**
+ * This is the query generated by clicking "New" and then choosing "Model" and "Use the notebook editor"
+ */
+export function startNewModel() {
+  const hash = newCardHash("model");
+  cy.visit(`/model/query#${hash}`);
+}
+
 /**
  * This is the query generated by clicking "New" and then choosing "Metric".
  */
diff --git a/e2e/test/scenarios/models/reproductions.cy.spec.ts b/e2e/test/scenarios/models/reproductions.cy.spec.ts
index c3d83fcc272..740c4455d36 100644
--- a/e2e/test/scenarios/models/reproductions.cy.spec.ts
+++ b/e2e/test/scenarios/models/reproductions.cy.spec.ts
@@ -1,16 +1,22 @@
 import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
 import {
+  createNativeQuestion,
   createQuestion,
+  enterCustomColumnDetails,
+  entityPickerModal,
+  entityPickerModalTab,
+  hovercard,
+  join,
+  modal,
+  openNotebook,
   openQuestionActions,
   popover,
+  queryBuilderMain,
   restore,
-  hovercard,
-  createNativeQuestion,
+  saveQuestion,
+  startNewModel,
   tableHeaderClick,
-  openNotebook,
-  enterCustomColumnDetails,
   visualize,
-  saveQuestion,
 } from "e2e/support/helpers";
 import type { FieldReference } from "metabase-types/api";
 
@@ -389,3 +395,52 @@ describe("issue 39150", { viewportWidth: 1600 }, () => {
       .should("have.length", 3);
   });
 });
+
+describe("issue 41785", () => {
+  beforeEach(() => {
+    restore();
+    cy.signInAsNormalUser();
+    cy.intercept("POST", "/api/dataset").as("dataset");
+  });
+
+  it("does not break the question when removing column with the same mapping as another column (metabase#41785)", () => {
+    // it's important to create the model through UI to reproduce this issue
+    startNewModel();
+    entityPickerModal().within(() => {
+      entityPickerModalTab("Tables").click();
+      cy.findByText("Orders").click();
+    });
+    join();
+    entityPickerModal().within(() => {
+      entityPickerModalTab("Tables").click();
+      cy.findByText("Orders").click();
+    });
+    popover().findByText("ID").click();
+    popover().findByText("ID").click();
+
+    cy.findByTestId("run-button").click();
+    cy.wait("@dataset");
+
+    cy.button("Save").click();
+    modal().button("Save").click();
+
+    cy.findByTestId("loading-indicator").should("exist");
+    cy.findByTestId("loading-indicator").should("not.exist");
+
+    cy.findByTestId("viz-settings-button").click();
+    cy.findByTestId("chartsettings-sidebar").within(() => {
+      cy.findAllByText("Tax").should("have.length", 1);
+      cy.findAllByText("Orders → Tax").should("have.length", 1);
+
+      cy.findByRole("button", { name: "Add or remove columns" }).click();
+      cy.findAllByText("Tax").should("have.length", 1);
+      cy.findAllByText("Orders → Tax").should("have.length", 1).click();
+    });
+
+    cy.wait("@dataset");
+
+    queryBuilderMain()
+      .findByText("There was a problem with your question")
+      .should("not.exist");
+  });
+});
-- 
GitLab