Skip to content

Error with enum names that are CamelCase #110

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

Open
Apteryx0 opened this issue Apr 17, 2025 · 0 comments · May be fixed by #111
Open

Error with enum names that are CamelCase #110

Apteryx0 opened this issue Apr 17, 2025 · 0 comments · May be fixed by #111

Comments

@Apteryx0
Copy link

Apteryx0 commented Apr 17, 2025

Generating a migration like

            op.sync_enum_values(
                enum_schema='public',
                enum_name='CamelCase',
                new_values=['active', 'passive', 'banned'],
                affected_columns=[TableReference(table_schema='public', table_name='users', column_name='status')],
                enum_values_to_rename=[],
            )

Will produce an error like below when running alembic upgrade heads

    op.sync_enum_values(
  File "<string>", line 8, in sync_enum_values
  File "<string>", line 3, in sync_enum_values
  File "/home/support/.local/lib/python3.11/site-packages/alembic_postgresql_enum/operations/sync_enum_values.py", line 190, in sync_enum_values
    cls._set_enum_values(
  File "/home/support/.local/lib/python3.11/site-packages/alembic_postgresql_enum/operations/sync_enum_values.py", line 85, in _set_enum_values
    create_comparison_operators(connection, enum_schema, enum_name, temporary_enum_name, enum_values_to_rename)
  File "/home/support/.local/lib/python3.11/site-packages/alembic_postgresql_enum/sql_commands/comparison_operators.py", line 79, in create_comparison_operators
    _create_comparison_operator(
  File "/home/support/.local/lib/python3.11/site-packages/alembic_postgresql_enum/sql_commands/comparison_operators.py", line 44, in _create_comparison_operator
    connection.execute(
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1385, in execute
    return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/sql/elements.py", line 334, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1577, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1953, in _execute_context
    self._handle_dbapi_exception(
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2134, in _handle_dbapi_exception
    util.raise_(
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/util/compat.py", line 211, in raise_
    raise exception
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1910, in _execute_context
    self.dialect.do_execute(
  File "/pipedream/local/venv/deepfield-env/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject) type public.CamelCase_old does not exist
​
[SQL:
            CREATE FUNCTION new_old_not_equals(
                new_enum_val "public"."CamelCase", old_enum_val "public"."CamelCase_old"
            )
            RETURNS boolean AS $$
                SELECT new_enum_val::text != old_enum_val::text;
            $$ LANGUAGE SQL IMMUTABLE
        ]
(Background on this error at: https://sqlalche.me/e/14/f405)

This is because earlier this occurred - note the lack of quotes around CamelCase_old even though they got used above (also this seemed to not register an error - so it got renamed to something else?? ¯\_(ツ)_/¯ ):

2025-04-11 18:41:15,602 INFO sqlalchemy.engine.Engine ALTER TYPE "public"."CamelCase" RENAME TO CamelCase_old
2025-04-11 18:41:15,602 alembic[107459][INFO][sqlalchemy.engine.Engine]: ALTER TYPE "public"."CamelCase" RENAME TO CamelCase_old
2025-04-11 18:41:15,602 INFO sqlalchemy.engine.Engine [generated in 0.00022s] {}
2025-04-11 18:41:15,602 alembic[107459][INFO][sqlalchemy.engine.Engine]: [generated in 0.00022s] {}
2025-04-11 18:41:15,603 INFO sqlalchemy.engine.Engine CREATE TYPE "public"."CamelCase" AS ENUM('example1', 'flowspec', 'dms_ace', 'hello')
2025-04-11 18:41:15,603 alembic[107459][INFO][sqlalchemy.engine.Engine]: CREATE TYPE "public"."CamelCase" AS ENUM('active', 'passive', 'banned')

Quote the enum name in the ALTER TYPE and everything is happy.....

@Apteryx0 Apteryx0 changed the title Error with enum names that are camel-case Error with enum names that are CamelCase Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant