Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

WRK-907: [Bug][Data Editing] Do not offer to 'Add option' in FK fields (#64304)

Stas Gavrylovf15ed1a7341d
diff --git a/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts b/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts
index 810e1c66fce..e0110fb53c8 100644
--- a/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts
+++ b/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts
@@ -6,7 +6,7 @@ import { resetSnowplow } from "e2e/support/helpers/e2e-snowplow-helpers";
const { H } = cy;
-const { ORDERS_ID, PRODUCTS_ID, PEOPLE_ID } = SAMPLE_DATABASE;
+const { ORDERS_ID, PRODUCTS_ID, PEOPLE_ID, ORDERS, PRODUCTS } = SAMPLE_DATABASE;
describe("scenarios > table-editing", () => {
beforeEach(() => {
@@ -499,6 +499,66 @@ describe("scenarios > table-editing", () => {
cy.findAllByTestId("row-edit-icon").should("exist");
});
});
+
+ describe("table editing bugs", () => {
+ it("WRK-907: should not allow to create new values for FK fields", () => {
+ cy.intercept("GET", `/api/table/${ORDERS_ID}/query_metadata`).as(
+ "getOrdersTable",
+ );
+ const NON_EXISTING_ID = "999999";
+ cy.intercept(
+ "GET",
+ `/api/field/${ORDERS.USER_ID}/search/${ORDERS.USER_ID}?value=${NON_EXISTING_ID}&limit=20`,
+ ).as("getFieldValues");
+ cy.intercept(
+ "GET",
+ `/api/field/${PRODUCTS.CATEGORY}/search/${PRODUCTS.CATEGORY}?value=${NON_EXISTING_ID}&limit=20`,
+ ).as("getCategoryValues");
+
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/edit`);
+
+ cy.wait("@getOrdersTable");
+
+ cy.findByTestId("new-record-button").click();
+
+ H.modal().within(() => {
+ cy.findByTestId("User ID-field-input").click();
+ cy.realType(NON_EXISTING_ID);
+ });
+
+ cy.wait("@getFieldValues");
+
+ H.popover().within(() => {
+ cy.findByText("Nothing found").should("be.visible");
+ cy.findByText(`Add option: ${NON_EXISTING_ID}`).should("not.exist");
+ });
+
+ H.modal().findByText("Cancel").click();
+
+ cy.intercept("GET", `/api/table/${PRODUCTS_ID}/query_metadata`).as(
+ "getProductsTable",
+ );
+
+ // navigate via breadcrumbs to avoid calling expensive `cy.visit`
+ cy.findByTestId("head-crumbs-container")
+ .findByText("Sample Database")
+ .click();
+ openTableEdit("Products");
+
+ cy.wait("@getProductsTable");
+
+ cy.findByTestId("new-record-button").click();
+
+ H.modal().within(() => {
+ cy.findByTestId("Category-field-input").click();
+ cy.realType(NON_EXISTING_ID);
+ });
+
+ H.popover()
+ .findByRole("option", { name: `Add option: ${NON_EXISTING_ID}` })
+ .should("be.visible");
+ });
+ });
});
function setTableEditingEnabledForDB(dbId: number, enabled = true) {
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx
index a5a4bea81de..71a6906b1ff 100644
--- a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx
@@ -1,3 +1,5 @@
+import { isFK } from "metabase-lib/v1/types/utils/isa";
+
import type { TableActionFormParameter } from "../api/types";
import { TableActionFormInputType } from "../api/types";
@@ -75,7 +77,9 @@ export function ParameterActionInput(props: ParameterActionInputProps) {
fieldId={parameter.field_id}
searchFieldId={parameter.human_readable_field_id}
isNullable={parameter.nullable}
- withCreateNew={!parameter.human_readable_field_id}
+ withCreateNew={
+ !parameter.human_readable_field_id && !isFK(parameter)
+ }
/>
);
}
diff --git a/frontend/src/metabase-lib/v1/types/utils/isa.js b/frontend/src/metabase-lib/v1/types/utils/isa.js
index 1dcc29c6b6b..4e69f2e9ced 100644
--- a/frontend/src/metabase-lib/v1/types/utils/isa.js
+++ b/frontend/src/metabase-lib/v1/types/utils/isa.js
@@ -22,9 +22,11 @@ import {
* @example
* isa(field.semantic_type, TYPE.Currency);
*
- * @param {string} x
- * @param {string} y
- * @return {boolean}
+ * @template X extends string
+ * @template Y extends string
+ * @param {X} x
+ * @param {Y} y
+ * @returns {x is Y}
*/
export const isa = (x, y) => cljs_isa(x, y);
@@ -120,6 +122,10 @@ export const isDimension = (col) => col && col.source !== "aggregation";
export const isMetric = (col) =>
col && col.source !== "breakout" && isSummable(col) && !hasNonMetricName(col);
+/**
+ * @param {Field | DatasetColumn} field
+ * @returns {boolean}
+ */
export const isFK = (field) => field && isTypeFK(field.semantic_type);
export const isPK = (field) => field && isTypePK(field.semantic_type);
export const isEntityName = (field) =>