Skip to content

MySQL migration fails due to PostgreSQL-specific uuid_generate_v4() default in tenant_credit_pools #31295

@wwwfeng

Description

@wwwfeng

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

v1.11.4

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. Deploy Dify using MySQL as the database (Docker / docker-compose).
  2. Initialize the database.
  3. Run flask db upgrade.
  4. Migration 7df29de0f6be_add_credit_pool fails when creating table tenant_credit_pools.

When using MySQL as the database backend, the migration for tenant_credit_pools fails due to a PostgreSQL-specific default value.

The migration and model definition use:

server_default = uuid_generate_v4()

However, uuid_generate_v4() is a PostgreSQL-only function and is not supported by MySQL, which causes the following error during migration:

  File "/app/api/.venv/lib/python3.12/site-packages/pymysql/connections.py", line 1203, in read
    first_packet = self.connection._read_packet()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/pymysql/connections.py", line 782, in _read_packet
    packet.raise_for_error()
  File "/app/api/.venv/lib/python3.12/site-packages/pymysql/protocol.py", line 219, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "/app/api/.venv/lib/python3.12/site-packages/pymysql/err.py", line 150, in raise_mysql_exception
    raise errorclass(errno, errval)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (3770, "Default value expression of column 'id' contains a disallowed function: `uuid_generate_v4`.")
[SQL: 
CREATE TABLE tenant_credit_pools (
        id CHAR(36) NOT NULL DEFAULT (uuid_generate_v4()), 
        tenant_id CHAR(36) NOT NULL, 
        pool_type VARCHAR(40) NOT NULL DEFAULT 'trial', 
        quota_limit BIGINT NOT NULL, 
        quota_used BIGINT NOT NULL, 
        created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, 
        updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, 
        CONSTRAINT tenant_credit_pool_pkey PRIMARY KEY (id)
)

]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

This makes a fresh MySQL installation fail during flask db upgrade.

✔️ Expected Behavior

Migrations should work correctly on MySQL without requiring PostgreSQL-specific functions.
UUIDs should be generated at the application level for MySQL deployments.

❌ Actual Behavior

The migration fails with a MySQL error because uuid_generate_v4() is used as a server-side default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞 bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions