Help Needed with Foreign Key Basics

I am brand new to Retool, SQL, etc (no other coding experience). I've taken some basics tutorials on SQL queries. Trying to get some simple tables and forms going, but really stuck on a foreign key issue, which is exclusively a result of my inexperience.

These are My Tables and columns

  • Parts
    • id
    • part_name,
    • part_number, etc
    • fkey_machine = foreign key column referencing machine_name column of Machines - a part can be assigned to multiple machines
  • Machines
    • id
    • machine_name

I've got a table component (parts_table) on my page for viewing parts, which includes the fkey_machine column, all of which is displaying correctly. I have a form component with all the relevant fields for parts in the parts_table, including a multi-select (machines_multiselect) for the fkey_machine column. I have the dropdown for the multi-select populating from the Machines table with my fetch_machines query.

The issues I'm having are:

  1. I can't get machines_multiselect to display the current fkey_machine value for the selected row in the table. All other fields show in the form upon row selection.

    • Data source = fetch_machines,
    • value = item.id
    • label = item.machine_name
    • Default value = parts_table.selectedRow.machines
  2. I can't get machines data from the form to update in the table. All other fields submit and update correctly. This is my update_parts query, with an event handler to refresh the parts table.

    SET
    part_name = {{ part_name_field.value }},
    part_number = {{ part_number_field.value }},
    qt_in_stock = {{ qt_stock_field.value }},
    markup = {{ markup_field.value }},
    min_stock = {{ min_stock_field.value }},
    retail_price = {{ retail_price_field.value }},
    fkey_machines = (
    SELECT
    machine_name
    FROM
    machines
    WHERE
    machine_name =ANY ({{ machines_multiselect.value }})
    )
    WHERE
    id = {{ parts_table.selectedRow.id }};`

Any help would be appreciated. I'm banging my head against the wall here.

Forgot to mention that I'm using Retool DB

Have you tried using chatgpt? Also, your update query should be UPDATE tablenamehere SET ..... Also, look into using Bulk Update and the GUI in the docs as your query may not be needed and could help you move a bit faster. :slight_smile:

Hey @doublewheels! Welcome to Retool and to the community we have here. :slightly_smiling_face:

Can you share a screenshot showing the configuration of the fkey_machine column? It sounds like you intended for it to store an array of strings, but I'm not sure that's possible via the Retool UI.

I have a feeling that the other issues you've described are both related to the formatting of that column.