https://discord.gg/botpress logo
what is the best method to connecting tables with ...
# 馃help
c
If I have two tables, say Users and Orders, and I want a field in the Orders table to connect to specific rows in the Users table, is a foreign key the right way to go? For example, an order_user_id in Orders that references the user_id in Users?
l
botpress tables do not have keys, but you can have the same field in both the tables and then get record from one table and use that record to query the second table, append the same array to have the full structure
c
I think I will using UUID ( integration simplygreatbot from integration hub ) And I will dealing with it like I do with foreign keys in supabase
l
not sure i understand
c
@limited-pencil-78283 You know how in a proper SQL database like Supabase, we link tables together using Foreign Keys? We're doing the exact same conceptual thing in Botpress, we just have to build the relationship ourselves since Botpress doesn't enforce it for us. Let me break it down with an example. 1. The Supabase Way (For Comparison) In Supabase, if we have a users table and an orders table, we'd define a relationship like this: 路 users table 路 id (UUID, Primary Key) | name (Text) 路 a1b2-c3d4... | Alice 路 e5f6-g7h8... | Bob 路 orders table 路 id (UUID, Primary Key) | product_name (Text) | user_id (UUID, Foreign Key) 路 o1o2-o3o4... | Super Widget | a1b2-c3d4... (This links to Alice) 路 o5o6-o7o8... | Mega Gizmo | e5f6-g7h8... (This links to Bob) 路 o9o0-p1p2... | Mini Thingy | a1b2-c3d4... (This is also Alice's order) The user_id in the orders table points to the id in the users table. This is a foreign key. To get all of Alice's orders, we'd run a JOIN query. 2. The Botpress Way (my Vision - It's the Same Concept!) In Botpress, we don't have automatic foreign key constraints, but we can emulate the pattern 100%. We use the UUID Integration card to generate a unique ID for a user and then treat that ID as our primary key. Step 1: Create the "Users" Table (or equivalent) You have a flow where a user signs up.You use the UUID Integration card to generate a unique user_id for them (e.g., a1b2-c3d4...). You save this ID along with their info in Botpress's native database (key-value store). 路 Key: user/a1b2-c3d4... 路 Value:
Copy code
json
  {
    "name": "Alice",
    "email": "alice@example.com",
    "user_id": "a1b2-c3d4..."
  }
Step 2: Create the "Orders" Table and Use the user_id as a Foreign Key Now,when Alice buys something, you create a new record for her order. The crucial part is that you store her user_id with the order. 路 Key: order/o1o2-o3o4... (you can generate another UUID for the order ID)
l
Yes - I understand what you want to achieve, but botpress tables do not have the concept of keys. However, you can achieve this programmatically using execute code card
c
Yes of course I will deal with that by using code cards But I ask the question to know if there is another optimal method
l
not that i am aware of
4 Views