langflow icon indicating copy to clipboard operation
langflow copied to clipboard

Foreign keys not working properly in SQLite

Open AntonioABLima opened this issue 1 year ago • 2 comments

Bug Description

When creating a flow through the POST /api/v1/flows/ endpoint, I accidentally included an old folder_id in the request body. This folder_id doesn’t exist in the current application.

Even though the flow was successfully created (status 201 Created), it didn’t appear in the UI. I was only able to find it using the api: GET /api/v1/flows/?folder_id={non_existing_folder_id}

Reproduction

Steps to reproduce:

  1. Send a POST request to /api/v1/flows/ with a body containing a folder_id that does not exist but has the right format.

    The example below uses a minimal payload (default empty flow):

 curl --location 'http://localhost:7860/api/v1/flows/' \
 --header 'Content-Type: application/json' \
 --header 'Accept: application/json' \
 --header 'x-api-key: sk-xxxxxxxxxxxxxxxx' \
 --data '{
     "name": "New Flow",
     "data": {
         "nodes": [],
         "edges": [],
         "viewport": {
             "zoom": 1,
             "x": 0,
             "y": 0
         }
     },
     "description": "Graph Your Way to Great Conversations.",
     "is_component": false,
     "folder_id": "e76a5de1-9a6c-47ce-82d1-50eae943c372",
     "icon": null,
     "gradient": null,
     "endpoint_name": null,
     "tags": [],
     "mcp_enabled": true
 }'
  1. Observe that the API returns 201 Created.
  2. Open the UI and check the flows list — the new flow is not visible.
  3. Use the API to GET /api/v1/flows/?folder_id={non_existing_folder_id} and confirm that the flow was actually created.

Expected behavior

Expected behavior: If the provided folder_id does not exist, the API should either:

  • Return an error (e.g., 400 Bad Request), or
  • Automatically assign the flow to a valid default folder (e.g., “root” or “unassigned”).

Actual behavior:

  • The flow is created successfully.
  • It’s not visible in the UI.
  • It can only be retrieved by querying the non-existing folder_id via the API.

Who can help?

No response

Operating System

WSL: Ubuntu 22.04

Langflow Version

1.6.4 and 1.6

Python Version

3.10

Screenshot

No response

Flow File

No response

AntonioABLima avatar Oct 17 '25 20:10 AntonioABLima

@AntonioABLima,

We have an environment variable that controls the SQLite settings:

LANGFLOW_SQLITE_PRAGMAS = {"synchronous": "NORMAL", "journal_mode": "WAL", "foreign_keys": "ON"}

If you include the property "foreign_keys": "ON", you may encounter errors when working with your JSON data.

By default, foreign key constraints are disabled in SQLite, so you need to enable them manually. In contrast, databases like PostgreSQL have foreign keys enabled by default.

Thank you for your support.

{"detail":"(sqlite3.IntegrityError) FOREIGN KEY constraint failed\n[SQL: INSERT INTO flow (name, description, icon_bg_color, gradient, is_component, updated_at, webhook, endpoint_name, data, mcp_enabled, action_name, action_description, access_type, id, user_id, icon, tags, locked, folder_id, fs_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]\n[parameters: ('New Flow (28)', 'Graph Your Way to Great Conversations.', None, None, 0, '2025-11-03 18:39:48.146557', 0, None, '{\"nodes\": [], \"edges\": [], \"viewport\": {\"zoom\": 1, \"x\": 0, \"y\": 0}}', 1, None, None, 'PRIVATE', 'f58d51431e2b470d98ef91373e5d66e0', 'f876ca39f169452cb10071d937628915', None, '[]', 0, 'e76a5de19a6c47ce82d150eae943c372', None)]\n(Background on this error at: https://sqlalche.me/e/20/gkpj)"}

Cristhianzl avatar Nov 03 '25 19:11 Cristhianzl

Thank you @Cristhianzl! For anyone who wants to set the variable manually, you can use:

export LANGFLOW_SQLITE_PRAGMAS='{"synchronous": "NORMAL", "journal_mode": "WAL", "foreign_keys": "ON"}'

AntonioABLima avatar Nov 03 '25 23:11 AntonioABLima