Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on startup with Postgres - constraint "fk_flow_user_id_user" for relation "flow" already exists #5371

Open
devinbost opened this issue Dec 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@devinbost
Copy link
Contributor

Bug Description

LangFlow is crashing on startup when postgres is enabled. It's giving this exception:

[12/19/24 18:47:43] ERROR    2024-12-19 18:47:43 - ERROR    - b2fa308044b5_add_unique_constraints - Error during upgrade: (psycopg2.errors.DuplicateObject)    b2fa308044b5_add_unique_constraints.py:51
                             constraint "fk_flow_user_id_user" for relation "flow" already exists                                                                                                       
                                                                                                                                                                                                        
                             [SQL: ALTER TABLE flow ADD CONSTRAINT fk_flow_user_id_user FOREIGN KEY(user_id) REFERENCES "user" (id)]                                                                    
                             (Background on this error at: https://sqlalche.me/e/20/f405)Traceback (most recent call last):  

I've experienced this on several of the recent LangFlow versions (starting I think around 1.0.19).

Interestingly, it's saying it's trying to upgrade, but the data directories are totally clear.

Detailed log is here:
docker-compose-postgres-alembic2.log

Reproduction

  1. Start LangFlow with this docker-compose.yaml file:
version: "0.4.12"

networks:
  langflow-network:
    driver: bridge

services:
  langflow3:
    image: langflowai/langflow-ep:v1.1.1.dev11 
    ports:
      - "7860:7860"
    command: ["langflow", "run", "--env-file", "/app/.env", "--host", "0.0.0.0", "--port", "7860"]
    working_dir: /app
    environment:
      - LANGFLOW_DATABASE_URL=postgresql://langflow:langflow@postgres:5432/langflow
      - COLUMNS=200
    volumes:
      - ./langflow-tmp/langflow-data:/app/langflow
      - ./langflow-tmp/langflow-app-data:/app/data
      - ./langflow-tmp/cache:/app/data/.cache
    networks:
      - langflow-network

  postgres:
    image: postgres:16
    environment:
      POSTGRES_USER: langflow
      POSTGRES_PASSWORD: langflow
      POSTGRES_DB: langflow
    ports:
      - "5432:5432"
    volumes:
      - ./langflow-tmp/langflow-postgres:/var/lib/postgresql/data
    networks:
      - langflow-network

Expected behavior

LangFlow should start as expected

Who can help?

@Jord

Operating System

Mac M1

Langflow Version

langflowai/langflow-ep:v1.1.1.dev11

Python Version

3.10

Screenshot

No response

Flow File

No response

@devinbost devinbost added the bug Something isn't working label Dec 19, 2024
Copy link

dosubot bot commented Dec 19, 2024

To resolve the startup crash in LangFlow with Postgres due to the "fk_flow_user_id_user" constraint already existing, you can modify the Alembic migration script to check for the existence of the foreign key constraint before attempting to create it. Here's a snippet that includes such a check:

flow_fks = {fk["name"] for fk in inspector.get_foreign_keys("flow")}

with op.batch_alter_table("flow", schema=None) as batch_op:
    if "flow_user_id_fkey" not in flow_fks:
        batch_op.create_foreign_key("flow_user_id_fkey", "user", ["user_id"], ["id"])

This modification ensures that the script checks for the existence of the foreign key constraint directly, preventing the attempt to create it if it already exists. Make sure to replace any outdated constraint names with the correct ones used in your database schema, such as "flow_user_id_fkey" instead of "fk_flow_user_id_user" [1][2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@AQK99
Copy link

AQK99 commented Dec 20, 2024

Hi, @ogabrielluiz I see that you worked last in the alembic. I'm getting the same issue. could you give some insight?

I'm not starting langflow from the docker, i'm trying to run it locally, and added the url to the .env file

@AQK99
Copy link

AQK99 commented Dec 20, 2024

@devinbost I was able to fix this by updating the upgrade() function in /langflow/src/backend/base/langflow/alembic/versions/b2fa308044b5_add_unique_constraints.py to this:

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    inspector = sa.inspect(conn)  # type: ignore
    tables = inspector.get_table_names()
    # ### commands auto generated by Alembic - please adjust! ###
    try:
        if "flowstyle" in tables:
            op.drop_table("flowstyle")
        with op.batch_alter_table("flow", schema=None) as batch_op:
            flow_columns = [column["name"] for column in inspector.get_columns("flow")]
            if "is_component" not in flow_columns:
                batch_op.add_column(sa.Column("is_component", sa.Boolean(), nullable=True))
            if "updated_at" not in flow_columns:
                batch_op.add_column(sa.Column("updated_at", sa.DateTime(), nullable=True))
            if "folder" not in flow_columns:
                batch_op.add_column(sa.Column("folder", sqlmodel.sql.sqltypes.AutoString(), nullable=True))
            if "user_id" not in flow_columns:
                batch_op.add_column(sa.Column("user_id", sqlmodel.sql.sqltypes.types.Uuid(), nullable=True))
            indices = inspector.get_indexes("flow")
            indices_names = [index["name"] for index in indices]
            if "ix_flow_user_id" not in indices_names:
                batch_op.create_index(batch_op.f("ix_flow_user_id"), ["user_id"], unique=False)
            existing_fks = [fk["name"] for fk in inspector.get_foreign_keys("flow")]
            if "fk_flow_user_id_user" not in existing_fks:
                batch_op.create_foreign_key("fk_flow_user_id_user", "user", ["user_id"], ["id"])

    except Exception as e:
        logger.exception(f"Error during upgrade: {e}")
        pass
        ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants